Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: writing to a file

by joeface (Pilgrim)
on Jul 01, 2005 at 14:55 UTC ( #471745=note: print w/replies, xml ) Need Help??


in reply to writing to a file

You can use Tie::File to treat the file like an array, then use unshift to add to the beginning of the array
use Tie::File; my $file = 'filename'; my @tiedfile; my $new_content = "foo"; ## Whatever you want to prepend to the file tie(@tiedfile, 'Tie::File', $file); unshift(@tiedfile, $new_content); untie(@tiedfile);
If you need to add more than one line, you can get your new content into an array and loop through it:
foreach my $line (reverse(@new_content)) { unshift(@tiedfile, $line); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://471745]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2017-02-21 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (309 votes). Check out past polls.