Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: inline file modification

by joeface (Pilgrim)
on Sep 07, 2005 at 17:50 UTC ( #489953=note: print w/ replies, xml ) Need Help??


in reply to inline file modification

Look at the Tie::File module. It allows you to edit a file in-place as if it were an array.

This example replaces any line containing $idtag with a line containing $replacement.

use Tie::File; my $file = 'filename'; my @tied_file; my $idtag = 'idTag'; my $replacement = 'replacement line'; # Create the array @tied_file from $file tie(@tied_file, 'Tie::File', $file); # Preserve the index so we can work directly on lines of @tied_file foreach my $i (0 .. $#tied_file) { if ($tied_file[$i] =~ /$idtag/) { # Do something to this line of @tied_file $tied_file[$i] = $replacement; } } untie(@tied_file);


Comment on Re: inline file modification
Download Code
Re^2: inline file modification
by Woodchuck (Initiate) on Sep 07, 2005 at 18:17 UTC
    Thank you, this is just the bit I needed. I suggest you put this in the snippets barn,as it might prove to be useful to a lot of people.
    keyword-> read write file
    Thanks again!

    -Chuck

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (15)
As of 2014-07-24 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (162 votes), past polls