home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_utl.zip / pl2pm.cmd < prev    next >
OS/2 REXX Batch file  |  1997-11-28  |  5KB  |  352 lines

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