perldoc perlrun says:#How does the print operator know what file handle to print to +? #Was this selected somewhere by magic? #Where is this documented / explained?
From the shell, saying $ perl -p -i.orig -e "s/foo/bar/; ... " is the same as using the program: #!/usr/bin/perl -pi.orig s/foo/bar/; which is equivalent to #!/usr/bin/perl $extension = '.orig'; LINE: while (<>) { if ($ARGV ne $oldargv) { if ($extension !~ /\*/) { $backup = $ARGV . $extension; } else { ($backup = $extension) =~ s/\*/$ARGV/g; } rename($ARGV, $backup); open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } s/foo/bar/; } continue { print; # this prints to original filename } select(STDOUT); except that the -i form doesn't need to compare $ARGV to $ +oldargv to know when the filename has changed. It does, however, +use ARGVOUT for the selected filehandle. Note that STDOUT is +restored as the default output filehandle after the loop.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
In reply to Re: how does print know what filehandle to use, when in a diamond in-place edit loop?
by merlyn
in thread how does print know what filehandle to use, when in a diamond in-place edit loop?
by tphyahoo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |