Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Quick Question - Append File at Beginning?

by jdlev (Scribe)
on Nov 16, 2013 at 00:52 UTC ( #1062829=perlquestion: print w/ replies, xml ) Need Help??
jdlev has asked for the wisdom of the Perl Monks concerning the following question:

I know you can append a file with ">>File.txt", but anything new that is added is added to the end of the file. Is there anyway to add the new stuff to the top of the file instead of the bottom?
I love it when a program comes together - jdhannibal

Comment on Quick Question - Append File at Beginning?
Re: Quick Question - Append File at Beginning?
by davido (Archbishop) on Nov 16, 2013 at 01:01 UTC

    What problem are you solving with this?

    Would it be adequate to append to the bottom of the file (rather than prepending before the start of the file), and then to read the end of the file with File::ReadBackwards, or File::Tail?


    Dave

Re: Quick Question - Append File at Beginning?
by Anonymous Monk on Nov 16, 2013 at 01:06 UTC
    No, not with any combination of open mode flags, only by opening a new file, appending old file to new file, renaming new file to old file
Re: Quick Question - Append File at Beginning?
by Kenosis (Priest) on Nov 16, 2013 at 01:44 UTC

    Is there anyway to add the new stuff to the top of the file instead of the bottom?

    Perhaps the following will be helpful:

    use strict; use warnings; addStuffToTopOfFile( "New Stuff!\n", 'File.txt' ); sub addStuffToTopOfFile { my ( $stuff, $file ) = @_; local @ARGV; local $^I = '.bak'; push @ARGV, $file; while (<>) { print $stuff if $. == 1; print; } unlink "$file.bak"; }

    File contents before running script:

    Line 1 Line 2 Line 3 Line 4

    File contents after running script:

    New Stuff! Line 1 Line 2 Line 3 Line 4

    This solution was implicit in Anonymous Monk's reply, but you send a string and file you want prepended to the subroutine. The script makes, and then deletes, a backup file during the process.

Re: Quick Question - Append File at Beginning?
by dasgar (Deacon) on Nov 16, 2013 at 01:46 UTC

    If you use Tie::File to 'tie' a file to an array, you can prepend to the file by unshifting to the array.

Re: Quick Question - Append File at Beginning?
by GrandFather (Cardinal) on Nov 16, 2013 at 02:08 UTC

    Following up on davido's question somewhat, if what you are trying to achieve is prepending data rather than text, maybe you would be better off considering using a database? They seem scary, or maybe just weird if you haven't used them before, but they can make a lot of data manipulation much easier and faster than trying to figure out how to roll the code from scratch.

    True laziness is hard work
Re: Quick Question - Append File at Beginning?
by tangent (Deacon) on Nov 16, 2013 at 02:42 UTC
    Following on from above suggestions you could have a look at the DB_BTREE format of DB_File. This stores keys in a given order so you could use timestamps as your keys and a $b <=> $a compare function, and be able to retrieve values in a last in first out style. Much easier to implement than a database.
Re: Quick Question - Append File at Beginning?
by NetWallah (Abbot) on Nov 16, 2013 at 04:22 UTC
    As the saying goes - many problems can be solved by adding "A layer of abstraction".

    In this case, if you have a separate file that contains the sequence in which to read files,
    then your problem can be solved by putting the new information first, in the sequence.

                 When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

Re: Quick Question - Append File at Beginning?
by oiskuu (Pilgrim) on Nov 16, 2013 at 08:37 UTC
    "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:
      "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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-07-23 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (142 votes), past polls