Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Quick Question - Append File at Beginning?

by oiskuu (Friar)
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));


Comment on Re: Quick Question - Append File at Beginning?
Select or Download Code
Re^2: Quick Question - Append File at Beginning?
by hippo (Curate) 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"
      Bill
        Greetings,

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

        Just a thought.

        --Chris

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-11-27 16:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (186 votes), past polls