Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: writing to the top of a file

by broquaint (Abbot)
on May 20, 2004 at 02:57 UTC ( #354836=note: print w/ replies, xml ) Need Help??


in reply to writing to the top of a file

Due to the nature of the structure of files you can't write to the top of them. So a simple solution is to create a new file and overwrite the old file e.g

open( my $old, '<', 'current.log' ) or die "ack: $!"; open( my $new, '>', 'current.log.new' ) or die "ack: $!"; print {$newfh} $your_data_here; print {$newfh} $_ while <$old>; close $old; close $new; rename 'current.log.new', 'current.log';
HTH

_________
broquaint


Comment on Re: writing to the top of a file
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2015-07-03 17:20 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 (54 votes), past polls