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