home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / pl2pm.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  4.6 KB  |  360 lines

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