Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: how to search and replace some text in a .conf file

by koti688 (Sexton)
on May 18, 2009 at 18:28 UTC ( [id://764695]=note: print w/replies, xml ) Need Help??


in reply to Re: how to search and replace some text in a .conf file
in thread how to search and replace some text in a .conf file

i am trying with this
$input = "svs.conf"; open IN, "+> $input" or die "Unable to open $input for writing, $!,sto +pped"; while ( <IN> ) { s/remote_be_address/192.168.106.60/g; } close IN;
Still not getting the correct result. :(

Replies are listed 'Best First'.
Re^3: how to search and replace some text in a .conf file
by Corion (Patriarch) on May 18, 2009 at 18:39 UTC

    You should also output your text somewhere. Look at print, or, if you want to do an in-place change, potentially look at Tie::File.

Re^3: how to search and replace some text in a .conf file
by GrandFather (Saint) on May 18, 2009 at 21:13 UTC

    There were a few things in punch_card_don that really should have been tidied up before being presented as sample code to someone new to Perl.

    First off, always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

    Second, always use the three parameter form of open. The intent is clearer and it's not subject to the security issues that the two parameter form is prone to.

    Always use lexical file handles (open my $in, '<', $input or die ...;).

    And avoid slurping files. Actually that one you sorted out for yourself - well done!

    Something to consider - editing a variable record length file in place doesn't work because you can't adjust the size of an individual record without rewriting all the records that follow it. A text file is generally a variable length record file type - each line is a record. About the only way around the problem is to make an edited copy of the original file, then replace the original with the new file by judicious file deletion and renaming.


    True laziness is hard work
Re^3: how to search and replace some text in a .conf file
by punch_card_don (Curate) on May 18, 2009 at 18:45 UTC
    If I'm not mistaken, the +> operator in your OPEN opens the file for reading & writing, creating it if it doesn't yet exist, but truncating it if it already exists.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found