On Thu, 28 Jan 2016 07:41:15 -0800
Jim Gibson <j...@gibson.org> wrote:

> > On Jan 28, 2016, at 1:37 AM, Frank Larry <frankylarry2...@gmail.com> wrote:
> > 
> > Hi Team,
> > 
> >  could you please let me? i have a file which contains "Debug", i would
> > like to replace debug to "Error", when i ran the below program the out
> > showing Error message but how to save the output with new changes. Could
> > you please tell me how to fix it?  
> 
> The way to do this within a larger Perl program is to open a new output file,
> copy all of the possibly-modified lines to this file. Then you can rename the
> new file to the same name as the old file, and perhaps rename the old file as
> well and keep it around as a backup.
> 
> > 
> > open(FILE, "<filter.txt") or die "Can’t open $!\n”;   
> 
> The three-argument version of open is preferred here, and let’s put the file
> name in a variable and use a lexical variable for the file handle (untested):
> 
> my $filename = ‘filter.txt’;
> open( my $in, ‘<‘, $filename ) or die(“Can’t open $filename for reading: $!”);
> 
> # create a new file
> my $newfile = $filename . ‘.new’;
> open( my $out, ‘>’, $newfile ) or die(“Can’t create $newfile: $!”);
> 
> > 
> > while($line = <FILE>){  
> 
> while( $line = <$in> ) {
>  

In addition to all that:

1. Add "use strict;" and "use warnings;" at the beginning.

2. «while ($line = <$in>)» should be «while (my $line = <$in>)»

For more information see:

http://perl-begin.org/tutorials/bad-elements/
> > 
> >    print "Before substituting: ", $line ,"\n"; 
> >     $line =~ s/Debug/Error/g;     
> >     print "After substituting : ", $line , "\n”;   
> 
>       print $out $line;

This is easy to confuse with:

        print $out, $line;

As a result it is preferable to do:

        print {$out} $line;

Or in new versions enough of perl :

        $out->print($line);

Finally, there's also http://perldoc.perl.org/autodie.html .

Regards,

        Shlomi Fish

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to