http://www.perlmonks.org?node_id=965027


in reply to replacing text in multiple files

If you have (a version of) the unix/linux "cat" utility, you can use the "diamond" operator as suggested by Eliya's initial reply, and pipe the concatenated set of files to your script's STDIN -- for that matter, you can also just print to STDOUT, and use redirection to a file on the command line:
cat *.log | your_script > modified.log-data
With that sort of usage, you don't need to worry about opening or closing files in your code; just read, do stuff, and print:
#!/usr/bin/perl use strict; while (<>) { # do stuff here, then... print; }

Replies are listed 'Best First'.
Re^2: replacing text in multiple files
by Eliya (Vicar) on Apr 14, 2012 at 04:43 UTC
    cat *.log | your_script > modified.log-data

    Why use cat when Perl can do it all by itself?  With the added benefit of $ARGV letting you know which file you're working on, and being able to do in-place edits via -i / $^I:

    $ cat *.log bar1 bar2 bar3 baz1 baz2 baz3 foo1 foo2 foo3 $ perl -ne 'print "$ARGV: $_"' *.log bar.log: bar1 bar.log: bar2 bar.log: bar3 baz.log: baz1 baz.log: baz2 baz.log: baz3 foo.log: foo1 foo.log: foo2 foo.log: foo3 $ cat *.log | perl -ne 'print "$ARGV: $_"' -: bar1 -: bar2 -: bar3 -: baz1 -: baz2 -: baz3 -: foo1 -: foo2 -: foo3 $ perl -i -ne 'print ucfirst $_' *.log $ cat *.log Bar1 Bar2 Bar3 Baz1 Baz2 Baz3 Foo1 Foo2 Foo3

    (note to the OP:  -n is the short "command line" version of saying while (<>) { ... } — see perl -h )