> "how do I tell perl to open a file for reading, do various
> commands, and then output those changes to a new file"???

Perl has some great one-liner shortcuts for writing filters.

First, -e lets you put perl on the command line:

    perl -e ' print "foo\n" '

Prints

    foo

Second, -p converts your perl to a filter:

    perl -pe ' s/foo/bar/ ' inputfiles

Replaces the first 'foo' on each line in inputfiles with 'bar'.
inputfiles can be omitted (in which case perl processes
stdin) or can be a single file or a filespec that matches
multiple files (in which case the files are processed one
after the other and the output is all written as one long
string to stdout).

Third, -i makes a filter work inplace, ie:

    perl -pie ' s/foo/bar/ ' *

Would filter all the files in the current directory, directly
changing them on disk. Dangerous! But easy!!

There are plenty of variations on the above to let you
create backups (relevant to the last one-liner), to use
these options on the first line of a regular script file, eg:

    #!/usr/bin/perl -i.bkp -p

For more on command line options, try:

    perl --help
    perldoc perlrun
    perldoc -f command

The Perl Cookbook is a fabulous source for all sorts
of practical tidbits like (well, much better) than the above.

Reply via email to