Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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
Replies are listed 'Best First'.
Re^2: Quick Question - Append File at Beginning?
by hippo (Vicar) 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 having an uproarious good time at the Monastery: (5)
As of 2015-07-28 02:02 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 (251 votes), past polls