in reply to Re: Elegant Way of Inserting Text at the Start of the File
in thread Elegant Way of Inserting Text at the Start of the File

Hi,

Thanks! But I want to insert this "code" into a script...

Eg. Something like this...

#!/usr/bin/perl while (<>) { BEGIN { print "Hello\n"; } print $_; } Then print Hello as the first line of text in a file.
So, this will print the Hello as the first line, but it means that I have to open the file, output the result to a new file, delete the old file, and rename the new file as the old file.

I was thinking if there is a way to just print "hello" straight to the file and incorporating it in a script?

Replies are listed 'Best First'.
Re^3: Elegant Way of Inserting Text at the Start of the File
by graff (Chancellor) on Nov 19, 2008 at 05:34 UTC
    The "BEGIN" block will always be executed when the script is loaded and before the first (non-BEGIN) instruction is executed. It doesn't matter where the "BEGIN" block is positioned in the code -- inside or outside the while loop makes no difference -- it will only be executed once.

    In order to add content at the beginning of any disk file while preserving all the original content, there really is no practical way other than to create a new file, put the newly added content at the beginning of that new file, then copy/append the contents of the existing file, then rename the new one to replace (delete) the old one.

    If you want to replace the initial portion of a file (or even any non-initial portion) while preserving the portion(s) that you don't replace, that can be done, but you have to be certain that the new data being put in has the exact same byte-count as the data being replaced.

    In other words, anything that involves moving the "preserved" content to a different byte offset (relative to the beginning of the file) is best done by writing a new version of the file to replace the old one.

Re^3: Elegant Way of Inserting Text at the Start of the File
by ikegami (Patriarch) on Nov 19, 2008 at 06:10 UTC

    So, this will print the Hello as the first line, but it means that I have to open the file, output the result to a new file, delete the old file, and rename the new file as the old file.

    Yes, you will have to do some variation of that. Like I said earlier, it's impossible to insert into a file.

    The most straightforward alternative is to load the file into memory, seek to the start of the file, then output the modified file. I guess that's where ig's code is useful.

Re^3: Elegant Way of Inserting Text at the Start of the File
by harishnuti (Beadle) on Nov 19, 2008 at 07:36 UTC

    i think you can use in-place edit to add lines anywhere in the file provided you know the line number for addition.
    the below code will add Hello in first line of file.
    #!/usr/bin/perl use warnings; use strict; my $string="Hello"; { local @ARGV = ("$ARGV[0]"); local $^I = '.bac'; while(<>){ if ($. == 1) { # if line no is 1 print "$string$/"; print; # Also print the current line } else { print; } } }

    if you name it edit.pl , then calling edit.pl <filename> will do the job.
    you can customize other stuff in the program, i just expressed idea.
    may be Monks can comment if its the right way or not, iam still a beginner and learner