Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 lurking in the Monastery: (4)
As of 2014-07-13 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls