Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Is something wrong with the below Script??

by koti688 (Sexton)
on May 19, 2009 at 04:11 UTC ( [id://764822]=perlquestion: print w/replies, xml ) Need Help??

koti688 has asked for the wisdom of the Perl Monks concerning the following question:

I am not able to substitute the desired string with this below Script
#!/usr/bin/perl $input = "koti.txt"; open IN, "+< $input" or die "Unable to open $input for reading, $!,sto +pped"; while ( <IN> ) { s/remote_be_address/192.168.106.60/g; print IN; } close IN;


Contents of koti.txt is like below

be_address = remote_be_address
be_address1 = remote_be_address
be_address2 = remote_be_address

Replies are listed 'Best First'.
Re: Is something wrong with the below Script??
by GrandFather (Saint) on May 19, 2009 at 05:12 UTC

    Yes:

    1. Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).
    2. Always use the three parameter version of open.
    3. Use lexical file handles: open my $in, '<', ...
    4. Indent controlled blocks (the body of the while loop in the sample code).
    5. Printing to a file you are actively reading from without an intervening seek is a bug.

    Consider:

    use strict; use warnings; my $testFileName = 'testFile.txt'; my $tempFileName = 'delme.txt'; # Create a sample file open my $out, '>', $testFileName or die "Failed to create $testFileNam +e: $!"; print $out <<END_STR; be_address = remote_be_address be_address1 = remote_be_address be_address2 = remote_be_address END_STR close $out; # The 'meat' open my $in, '<', $testFileName; open $out, '>', $tempFileName or die "Failed to create $tempFileName: +$!"; while (<$in>) { s/remote_be_address/192.168.106.60/g; print $out $_; } close $in or die "Failed to close $testFileName: $!"; close $out or die "Failed to close $tempFileName: $!"; unlink $testFileName; rename $tempFileName, $testFileName; # Show the result open $in, '<', $testFileName; print <$in>; close $in or die "Failed to close $testFileName: $!";

    Prints:

    be_address = 192.168.106.60 be_address1 = 192.168.106.60 be_address2 = 192.168.106.60

    True laziness is hard work
      rename will overwrite/clobber existing filename, so you can omit unlink call
Re: Is something wrong with the below Script??
by targetsmart (Curate) on May 19, 2009 at 04:41 UTC
    You can edit files in place using perl -i

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      According to perlrun, -i doesn't actually edit in-place, it opens 2 filehandles and does renaming :)
        That may be true, but does it matter?

        --
        use JAPH;
        print JAPH::asString();

Re: Is something wrong with the below Script??
by Anonymous Monk on May 19, 2009 at 04:15 UTC
    use strict; use warnings; my $infile = ... my $outfile = ... open IN ... open OUT ... while(<IN>){ ... print OUT $_; } close IN; close OUT; rename $infile, $outfile or die "COULDN'T RENAME ($infile) to $outfile +): $!";
      why another out File handler??,
      is it not possible to open the same file and replace the sting and save in to it. I need to save in the same file only ..

      Please help

        You might find seek helpful, and in particular this bit:

        Due to the rules and rigors of ANSI C, on some systems you have to do a seek whenever you switch between reading and writing.

        update: You should also think about what will happen to your file if you write directly to your original file and your modified string is shorter or longer than the original.

        Because its short, its easy to understand and it works. Editing a file in-place is possible, but its nontrivial. See Re: Edit File in place for some reasons why.
        It's a good idea to rename your original file to a new name, and to then write out the new version of the file to the original name. This provides an audit trail, and helps to resolve pesky questions when a change you didn't want occurrs...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://764822]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-25 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found