[Israel.pm] -i and more

Yossi.Itzkovich at ecitele.com Yossi.Itzkovich at ecitele.com
Mon Apr 19 05:09:56 PDT 2004

I already wrote that :"I can do it in the long way"  - I am just looking
for the shorter and TIMTOWTDI

                      "Offer Kaye"                                                                                                 
                      <oferk at oren.co.i         To:      "Perl in Israel" <perl at perl.org.il>                                        
                      l>                       cc:                                                                                 
                      Sent by:                 Subject: RE: [Israel.pm] -i and more                                                
                      perl-bounces at per                                                                                             
                      04/19/2004 15:59                                                                                             
                      Please respond                                                                                               
                      to Perl in                                                                                                   

> Hi,
> I need to write a scipt that gets:  inFileName outFileName oldStr  newStr
> The script should be a simple one that replaces all occurances of oldStr
> with newStr  from inFileName, and save it in outFileName
> I can do it in the long way or in the shorter (and much simpler ) way.
> My questions:
> 1.  the -i flag saves the output file with the original name
> +suffix/prefix.  In this case I need to use a name given by the user.
> 2.  The oldStr/newStr may contain wierd characters such as */\@
> etc.  I do
> I prevernt meta characters to affect my perl code ?
> Thanks
> Yossi

1. Since you are talking about a totally different name here, I don't see
how using the -i switch is usefull. It really isn't that long to write
something that opens 2 filehandles...
2. Use the "quotemeta" function, or the "\Q\E" quote operator.

Example code:
use warnings;
use strict;
my ($inFileName, $outFileName, $oldStr, $newStr) = @ARGV;
$oldStr = quotemeta $oldStr;
open(IN,"$inFileName") or die "Couldn't open $inFileName for reading:
open(OUT,">$outFileName") or die "Couldn't open $outFileName for writing:
while(<IN>) {
   print OUT;
close(IN) or die "Couldn't close $inFileName after reading: $!\n";
close(OUT) or die "Couldn't close $outFileName after writing: $!\n";

Offer Kaye
Perl mailing list
Perl at perl.org.il

More information about the Perl mailing list