CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
writing a line above in a file

Jan 13, 2000, 9:01 AM

Post #1 of 6 (4188 views)
writing a line above in a file Can't Post

Hi there,

if you open a file and just write something in it like this:
print FILE "this is a new line";

the new line will be placed below in the file. How to write it above the file ?



Jan 13, 2000, 11:49 AM

Post #2 of 6 (4188 views)
Re: writing a line above in a file [In reply to] Can't Post

open(FILE, "your.txt");
@text = <FILE>;
open(FILE, ">your.txt");
print FILE "This is the new line.\n";
print FILE "@text";

You could probably do it with some more complex piece of code but not much of a reason to.


Jan 13, 2000, 12:06 PM

Post #3 of 6 (4188 views)
Re: writing a line above in a file [In reply to] Can't Post

Printing an array in double quotes will insert a space (or whatever the value of $", the list separator variable, is) in between each element. Probably not wanted.

On the contrary, there's good reason to try and do it more efficiently. For one, on a large file, the code @text = <FH> slurps the entire file into memory, and that can be costly for large files.

First, the question is posed "why do you need to place data in the begninning of the file?" If you have a good enough reason, I suggest you look at perlfaq5, which has the question of placing data after (or before) a specific line of a file.

If you merely want to place it at the head of the file, try this efficient method:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open FILE, "+<$file" or die "can't open $file for read/write: $!";
$content = do { local($/), <FILE> };
seek FILE, 0, 0;
print FILE $to_add, $content;
close FILE;

Here's an explanation on how that code works. When opening a file, "+<filename" means the file is opened for reading, BUT it can also be written to. Then, I assign the ENTIRE file to a scalar, by setting the $/ variable to undef, which means the entire file is read as a single "line". Then, I use seek() to get back to beginning of the file, and print the new content first, followed by the original content of the file.

Again, I suggest you look at perlfaq5 for information on adding data after line X of a file.

[This message has been edited by japhy (edited 01-13-2000).]

Administrator / Moderator

Jan 13, 2000, 1:50 PM

Post #4 of 6 (4188 views)
Re: writing a line above in a file [In reply to] Can't Post

For your convenience, we've uploaded the Perl manpages here onto

You can find perlfaq5, referenced by japhy's excellent post, here.

Jan 15, 2000, 5:10 AM

Post #5 of 6 (4188 views)
Re: writing a line above in a file [In reply to] Can't Post

Thanks for your great help.

The reason of writing a line above in a file is.
I'm trying to make a download script, only for linking the files.

the newest files will be listed above !

I'm looking for some one who can help me with this. You could mail me at

Thanks !


Jan 15, 2000, 9:27 AM

Post #6 of 6 (4188 views)
Re: writing a line above in a file [In reply to] Can't Post

Ah, well, in your case, you don't need to write to the top of the file. If you append, like shown below, you can still do what you wish:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open LINKS, ">>links.txt" or die "can't append to links.txt: $!";
print LINKS "next link\n";
close LINKS;

# then later, to read them, NEWEST FIRST

open LINKS, "links.txt" or die "can't read links.txt: $!";
chomp, unshift @files, $_ while <LINKS>;
close LINKS;

This is a more efficient idea than:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

chomp(@files = <LINKS> );
@files = reverse(@files);

# or

chomp(@files = reverse(<LINKS> ));

Because like I said in the previous post, these require a list of all the lines of the file to be built all at once, which can take a lot of memory for a big file.

My solution uses the unshift() function, which places an element at the BEGINNING of an array, as opposed to push(), which puts the element at the end of the list. Don't be thrown by the while-loop looking kind of backwards:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

while (<LINKS> ) {
unshift @files, $_;
# or
# chomp, unshift @files, $_;

# is the same as

chomp, unshift @files, $_ while <LINKS>;

For simple code structures, you can put while(), until(), and if() at the end of a statement. You CAN'T do this, though:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

print "hello" if $name eq "Jeff";
print "goodbye" else;

That 'else' there isn't right... if you need if-else-elsif blocks, you have to do those in the regular style. Also, if you have a recent enough version of Perl, you can place for() or foreach() at the end of a statement:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

print "$_...\n" for reverse(1 .. 10);
print "blast off!\n";

I think that's enough for now. I've got to leave SOMETHING for another answer. Smile

[This message has been edited by japhy (edited 01-15-2000).]


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives