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); }


Comment on Re: writing to a file
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-07-29 07:06 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 (260 votes), past polls