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