tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:
Outputs:use strict; use warnings; use Test::More qw(no_plan); #create a.txt contining "a" open F, "> a.txt"; print F "a"; close F; #fails because no substitution was made #prints to STDOUT process_file(); local $^I = ".bak"; #now it works. #prints to "a.txt" as we want. But how does it know to print there? write_and_read_file(); #ok sub process_file { @ARGV = qw(a.txt); local $/; # slurp it #changes a.txt so contains "b" #the diamond operator operates on @ARGV by default while (<>) { #$_ now contains the entire file, alter at will s/a/b/g; #How does the print operator know what file handle to print to +? #Was this selected somewhere by magic? #Where is this documented / explained? print; } #need to reset @ARGV, I guess it got wiped out somewhere in that w +hile. @ARGV = qw(a.txt); while (<>) { is($_,"b"); } }
(Inspired by Re: Shell trick to edit many files with perl)bnot ok 1 # Failed test (diamond.pl at line 35) # got: 'a' # expected: 'b' Undefined subroutine &main::write_and_read_file called at diamond.pl l +ine 15, <> line 1. 1..1 # Looks like you failed 1 tests of 1. # Looks like your test died just after 1.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how does print know what filehandle to use, when in a diamond in-place edit loop?
by merlyn (Sage) on Aug 09, 2005 at 15:40 UTC | |
|
Re: how does print know what filehandle to use, when in a diamond in-place edit loop?
by gellyfish (Monsignor) on Aug 09, 2005 at 15:38 UTC | |
|
Re: how does print know what filehandle to use, when in a diamond in-place edit loop?
by ysth (Canon) on Aug 11, 2005 at 10:05 UTC |