David Sudjiman wrote:
> This is my script.
> 
> ===
> #!/usr/bin/perl
> # ex9-2b.pl
> 
> use strict;
> use warnings;
> 
> my ($filename) =  @ARGV;
> 
> open FILE, $filename or die "Can't open $filename : $!";
> 
> $^I = ".out";  
> while (<FILE>) {
>    s/Fred/Larry/i;
>    print;
> }
> ===
> 
> I executed using:
> $ perl ex9-2b.pl fred_larry.txt
> AlLarry
> Larrydy
> Larrydy Mercury
> Wilma
> wilma
> Larry
> Larrydo
> freedo
> Antonius AlLarry
> Mr. AlLarry
> Mr. Larrydy
> Mrs. Larrydy Mercury
> Ms. Wilma
> Ms. wilma
> Mr. Larry
> Mrs. Larrydo
> Mr. freedo
> Antonius J. AlLarry
> Larry
> Larry
> Larry
> Mr. Larry and Mrs. Wilma
> Larry and Wilma
> Wilma Larry
> Larrywilma
> 
> It does the work s/Fred/Larry/i but why didn't want to print back to the 
> file.
> 
> I'm using different approach like below:
> 
> ===
> #!/usr/bin/perl
> # ex9-e.pl
> 
> use strict;
> use warnings;
> 
> $^I = ".out";
> 
> while (<>) {
>    s/Fred/Larry/i;
>    print;
> }  
> ===
> 
> It does the replacement and print back. Why differs?

Because the null filehandle inside the readline operator ( <> ) has special
magic which works together with the @ARGV array and the $^I scalar but does
not work with ordinary filehandles.

See the "I/O Operators" section of perlop.pod for an explanation of the null
filehandle and the -i switch entry in perlrun.pod for an explanation of the
$^I variable.

perldoc perlop
perldoc perlrun


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to