Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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
Replies are listed 'Best First'.
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 romping around the Monastery: (7)
As of 2015-07-29 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (267 votes), past polls