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