Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Replacing a string in a file

by Avi (Acolyte)
on Jul 16, 2003 at 19:12 UTC ( [id://274987]=note: print w/replies, xml ) Need Help??


in reply to Re: Replacing a string in a file
in thread Replacing a string in a file

For opening the file for reading writing, how do you go about doing this? Do you have sample code? It is for a config file which is very small, so this solution would be great.

Thanks,
Avi

Replies are listed 'Best First'.
Re: Replacing a string in a file
by perlplexer (Hermit) on Jul 16, 2003 at 21:28 UTC
    Here's how I would approach this (not tested)
    use strict; use Fcntl ':flock'; my $iniFile = 'config.ini'; if (open my $ini, "+<$iniFile"){ if (flock $ini, LOCK_EX){ my @data = <$ini>; # Work with @data -- delete lines, update lines, etc. if (truncate($ini, 0) and seek($ini, 0, 0)){ print "Can't write to $iniFile : $!\n" unless print $ini @data; }else{ print "Can't truncate/seek : $!\n"; } }else{ print "Can't lock : $!\n"; } close $ini; }else{ print "Can't open $iniFile : $!\n"; }
    --perlplexer

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found