Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Quick Question - Append File at Beginning?

by oiskuu (Hermit)
on Nov 16, 2013 at 08:37 UTC ( #1062866=note: print w/replies, xml ) Need Help??

in reply to Quick Question - Append File at Beginning?

"Appending" to the beginning of a file. Is that a FAQ?

The operation you request is not typically supported by underlying filesystem. OS/400 I think supports record-based filesmembers where you can insert/delete with abandon? For ext4, etc., this operation will be both costly and inherently dangerous.

This said, in case you insist on DWIM, a simple mmap can do the trick, provided it is available.
Update: use map_handle instead of map_file.

no warnings; use File::Map ':all'; open(my $fh, '+>>', shift) or die; # open/mmap modes must ~~ my $off = $fh->tell; print $fh join " ", @ARGV; $fh->flush; # rotate in place map_handle(my $mmap, $fh, '+<'); substr($mmap, 0, 0, substr($mmap, $off));

Prev version:
no strict; no warnings; use File::Map ':all'; my $fnam = shift @ARGV or die; open(my $fh, '>>', $fnam) or die; my $off = -s $fh; print $fh join " ", @ARGV; close $fh; # ! # rotate in place map_file my $mmap, $fnam, '+<'; substr($mmap, 0, 0, substr($mmap, $off));

Replies are listed 'Best First'.
Re^2: Quick Question - Append File at Beginning?
by hippo (Abbot) on Nov 16, 2013 at 11:12 UTC
    "Appending" to the beginning of a file. Is that a FAQ?

    Indeed it is.

      I believe that the relavent FAQ is much easier to find with the tool perldoc.

      perldoc -q "append to the beginning of a file"

        How does *NIX MBOX deal with data? Would the way it does it, offer some interesting methods.

        Just a thought.


        #!/usr/bin/perl -Tw
        use Perl::Always or die;
        my $perl_version = (5.12.5);
        print $perl_version;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-04-23 06:12 GMT
Find Nodes?
    Voting Booth?