Inline Perl

From HalfgeekKB
Revision as of 07:04, 12 May 2005 by 161.253.9.181 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Batch processing using Perl

The -n switch in Perl forces the entire program to be interpreted as if in a while(<STDIN>) {} loop. More or less, this means the program is run once for each line of input, and the current line is in $_. Use chomp or a regexp to get rid of the trailing newline.

Here is how one might write ls *.js using Perl as the filter:

ls | perl -n -e '/^((.*)\.js)$/ or next; print "$1\n"'

To run pod2html on all .js files in a directory, writing the output for XXX.js to XXX.html, displaying each command used in the process, one could use a command like this:

ls | perl -n -e '/^((.*)\.js)$/ or next; $c="pod2html $1 >> $2"; print "\n$c\n"; print `$c`'