1#!/usr/local/bin/perl 2 3use strict; 4use warnings; 5 6use Config; 7use File::Basename qw(&basename &dirname); 8use Cwd; 9 10# List explicitly here the variables you want Configure to 11# generate. Metaconfig only looks for shell variables, so you 12# have to mention them as if they were shell variables, not 13# %Config entries. Thus you write 14# $startperl 15# to ensure Configure will look for $Config{startperl}. 16 17# This forces PL files to create target in same directory as PL file. 18# This is so that make depend always knows where to find PL derivatives. 19my $origdir = cwd; 20chdir dirname($0); 21my $file = basename($0, '.PL'); 22$file .= '.com' if $^O eq 'VMS'; 23 24open OUT, ">", $file or die "Can't create $file: $!"; 25 26print "Extracting $file (with variable substitutions)\n"; 27 28# In this section, perl variables will be expanded during extraction. 29# You can use $Config{...} to use Configure variables. 30 31print OUT <<"!GROK!THIS!"; 32$Config{startperl} 33 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}' 34 if 0; # ^ Run only under a shell 35!GROK!THIS! 36 37# In the following, perl variables are not expanded during extraction. 38 39print OUT <<'!NO!SUBS!'; 40 41=head1 NAME 42 43pl2pm - Rough tool to translate Perl4 .pl files to Perl5 .pm modules. 44 45=head1 SYNOPSIS 46 47B<pl2pm> F<files> 48 49=head1 DESCRIPTION 50 51B<pl2pm> is a tool to aid in the conversion of Perl4-style .pl 52library files to Perl5-style library modules. Usually, your old .pl 53file will still work fine and you should only use this tool if you 54plan to update your library to use some of the newer Perl 5 features, 55such as AutoLoading. 56 57=head1 LIMITATIONS 58 59It's just a first step, but it's usually a good first step. 60 61=head1 AUTHOR 62 63Larry Wall <larry@wall.org> 64 65=cut 66 67use strict; 68use warnings; 69 70my %keyword = (); 71 72while (<DATA>) { 73 chomp; 74 $keyword{$_} = 1; 75} 76 77local $/; 78 79while (<>) { 80 my $newname = $ARGV; 81 $newname =~ s/\.pl$/.pm/ || next; 82 $newname =~ s#(.*/)?(\w+)#$1\u$2#; 83 if (-f $newname) { 84 warn "Won't overwrite existing $newname\n"; 85 next; 86 } 87 my $oldpack = $2; 88 my $newpack = "\u$2"; 89 my @export = (); 90 91 s/\bstd(in|out|err)\b/\U$&/g; 92 s/(sub\s+)(\w+)(\s*\{[ \t]*\n)\s*package\s+$oldpack\s*;[ \t]*\n+/${1}main'$2$3/ig; 93 if (/sub\s+\w+'/) { 94 @export = m/sub\s+\w+'(\w+)/g; 95 s/(sub\s+)main'(\w+)/$1$2/g; 96 } 97 else { 98 @export = m/sub\s+([A-Za-z]\w*)/g; 99 } 100 my @export_ok = grep($keyword{$_}, @export); 101 @export = grep(!$keyword{$_}, @export); 102 103 my %export = (); 104 @export{@export} = (1) x @export; 105 106 s/(^\s*);#/$1#/g; 107 s/(#.*)require ['"]$oldpack\.pl['"]/$1use $newpack/; 108 s/(package\s*)($oldpack)\s*;[ \t]*\n+//ig; 109 s/([\$\@%&*])'(\w+)/&xlate($1,"",$2,$newpack,$oldpack,\%export)/eg; 110 s/([\$\@%&*]?)(\w+)'(\w+)/&xlate($1,$2,$3,$newpack,$oldpack,\%export)/eg; 111 if (!/\$\[\s*\)?\s*=\s*[^0\s]/) { 112 s/^\s*(local\s*\()?\s*\$\[\s*\)?\s*=\s*0\s*;[ \t]*\n//g; 113 s/\$\[\s*\+\s*//g; 114 s/\s*\+\s*\$\[//g; 115 s/\$\[/0/g; 116 } 117 s/open\s+(\w+)/open($1)/g; 118 119 my $export_ok = ''; 120 my $carp =''; 121 122 123 if (s/\bdie\b/croak/g) { 124 $carp = "use Carp;\n"; 125 s/croak "([^"]*)\\n"/croak "$1"/g; 126 } 127 128 if (@export_ok) { 129 $export_ok = "\@EXPORT_OK = qw(@export_ok);\n"; 130 } 131 132 if ( open(PM, ">", $newname) ) { 133 print PM <<"END"; 134package $newpack; 135use 5.006; 136require Exporter; 137$carp 138\@ISA = qw(Exporter); 139\@EXPORT = qw(@export); 140$export_ok 141$_ 142END 143 } 144 else { 145 warn "Can't create $newname: $!\n"; 146 } 147} 148 149sub xlate { 150 my ($prefix, $pack, $ident,$newpack,$oldpack,$export) = @_; 151 152 my $xlated ; 153 if ($prefix eq '' && $ident =~ /^(t|s|m|d|ing|ll|ed|ve|re)$/) { 154 $xlated = "${pack}'$ident"; 155 } 156 elsif ($pack eq '' || $pack eq 'main') { 157 if ($export->{$ident}) { 158 $xlated = "$prefix$ident"; 159 } 160 else { 161 $xlated = "$prefix${pack}::$ident"; 162 } 163 } 164 elsif ($pack eq $oldpack) { 165 $xlated = "$prefix${newpack}::$ident"; 166 } 167 else { 168 $xlated = "$prefix${pack}::$ident"; 169 } 170 171 return $xlated; 172} 173__END__ 174AUTOLOAD 175BEGIN 176CHECK 177CORE 178DESTROY 179END 180INIT 181UNITCHECK 182abs 183accept 184alarm 185and 186atan2 187bind 188binmode 189bless 190caller 191chdir 192chmod 193chomp 194chop 195chown 196chr 197chroot 198close 199closedir 200cmp 201connect 202continue 203cos 204crypt 205dbmclose 206dbmopen 207defined 208delete 209die 210do 211dump 212each 213else 214elsif 215endgrent 216endhostent 217endnetent 218endprotoent 219endpwent 220endservent 221eof 222eq 223eval 224exec 225exists 226exit 227exp 228fcntl 229fileno 230flock 231for 232foreach 233fork 234format 235formline 236ge 237getc 238getgrent 239getgrgid 240getgrnam 241gethostbyaddr 242gethostbyname 243gethostent 244getlogin 245getnetbyaddr 246getnetbyname 247getnetent 248getpeername 249getpgrp 250getppid 251getpriority 252getprotobyname 253getprotobynumber 254getprotoent 255getpwent 256getpwnam 257getpwuid 258getservbyname 259getservbyport 260getservent 261getsockname 262getsockopt 263glob 264gmtime 265goto 266grep 267gt 268hex 269if 270index 271int 272ioctl 273join 274keys 275kill 276last 277lc 278lcfirst 279le 280length 281link 282listen 283local 284localtime 285lock 286log 287lstat 288lt 289m 290map 291mkdir 292msgctl 293msgget 294msgrcv 295msgsnd 296my 297ne 298next 299no 300not 301oct 302open 303opendir 304or 305ord 306our 307pack 308package 309pipe 310pop 311pos 312print 313printf 314prototype 315push 316q 317qq 318qr 319quotemeta 320qw 321qx 322rand 323read 324readdir 325readline 326readlink 327readpipe 328recv 329redo 330ref 331rename 332require 333reset 334return 335reverse 336rewinddir 337rindex 338rmdir 339s 340scalar 341seek 342seekdir 343select 344semctl 345semget 346semop 347send 348setgrent 349sethostent 350setnetent 351setpgrp 352setpriority 353setprotoent 354setpwent 355setservent 356setsockopt 357shift 358shmctl 359shmget 360shmread 361shmwrite 362shutdown 363sin 364sleep 365socket 366socketpair 367sort 368splice 369split 370sprintf 371sqrt 372srand 373stat 374study 375sub 376substr 377symlink 378syscall 379sysopen 380sysread 381sysseek 382system 383syswrite 384tell 385telldir 386tie 387tied 388time 389times 390tr 391truncate 392uc 393ucfirst 394umask 395undef 396unless 397unlink 398unpack 399unshift 400untie 401until 402use 403utime 404values 405vec 406wait 407waitpid 408wantarray 409warn 410while 411write 412x 413xor 414y 415!NO!SUBS! 416 417close OUT or die "Can't close $file: $!"; 418chmod 0755, $file or die "Can't reset permissions for $file: $!\n"; 419exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':'; 420chdir $origdir; 421