<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.webmaster-forums.net/crss/node/1001035" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>https://www.webmaster-forums.net/crss/node/1001035</link>
    <description></description>
    <language>en</language>
          <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-top-file#comment-1004429</link>
    <description> &lt;p&gt;Writing to the beginning of a file is extremely inefficient.  You end up the following, and you probably forget to flock() the file.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;code:&lt;/p&gt;
&lt;pre&gt;
use Fcntl &#039;:flock&#039;;  # for LOCK_EX

open FILE, &quot;+&amp;lt;$file&quot; or die &quot;can&#039;t open $file for read/write: $!&quot;;
flock FILE, LOCK_EX;

if ($slurp_into_nasty_array) {
  @file = &amp;lt;FILE&amp;gt;;
  unshift @file, @new;
  seek FILE, 0, 0;
  print FILE @file;
}
elsif ($slurp_into_scalar) {
  # can also be nasty, for big files
  local $/;
  $file = &amp;lt;FILE&amp;gt;;
  seek FILE, 0, 0;
  print FILE @new, $file;
}
elsif ($while_loop) {
  push @file, $_ while &amp;lt;FILE&amp;gt;;
  seek FILE, 0, 0;
  print FILE @new, @file;
}
truncate FILE, tell FILE;  # for safety...
close FILE;
[/code]

But I seriously don&#039;t suggest you do this.  You should answer the question &quot;why do I need to write to the top of a file?&quot;  You can probably get around your problem by appending to the file, and reading the file&#039;s contents, but in reverse:

&lt;BLOCKQUOTE&gt;code:&lt;pre&gt;
use Fcntl &#039;:flock&#039;;
open FILE, &quot;&amp;gt;&amp;gt;$file&quot; or die &quot;can&#039;t append to $file: $!&quot;;
print FILE @new;
close FILE;

open FILE, $file or die &quot;can&#039;t read $file: $!&quot;;
unshift @lines, $_ while &amp;lt;FILE&amp;gt;;
close FILE;

# @lines is now the lines of $file in the REVERSE order (most recent addition first)
[/code]

Look into this method.  You&#039;ll be happier.

------------------
-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve &lt;/pre&gt;&lt;/blockquote&gt;&lt;/pre&gt;&lt;/blockquote&gt;</description>
     <pubDate>Thu, 23 Mar 2000 14:33:00 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1004429 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-top-file#comment-1004428</link>
    <description> &lt;p&gt;Sorry I didn&#039;t get back to you sooner with that, I was at school when I posted.&lt;/p&gt;
&lt;p&gt;***Senior Member?***&lt;/p&gt;
 </description>
     <pubDate>Tue, 21 Mar 2000 21:39:00 +0000</pubDate>
 <dc:creator>Orpheus</dc:creator>
 <guid isPermaLink="false">comment 1004428 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-top-file#comment-1004427</link>
    <description> &lt;p&gt;Darn, the people who help in this forum are excellent. Thanks alot for both or your help. I&#039;ll try it out and see if it works.&lt;/p&gt;
&lt;p&gt;------------------&lt;br /&gt;
&lt;strong&gt;Flame Hosting :&lt;/strong&gt; &lt;a href=&quot;http://www.flamehosting.com&quot; class=&quot;bb-url&quot;&gt;http://www.flamehosting.com&lt;/a&gt;&lt;br /&gt;
&lt;strong&gt;NT and UNIX Hosting, Dedicated Servers, and Co-Location Programs&lt;/strong&gt;&lt;/p&gt;
 </description>
     <pubDate>Tue, 21 Mar 2000 20:15:00 +0000</pubDate>
 <dc:creator>Justin S</dc:creator>
 <guid isPermaLink="false">comment 1004427 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-top-file#comment-1004426</link>
    <description> &lt;p&gt;Somthing like this will work&lt;br /&gt;
&lt;BLOCKQUOTE&gt;code:&lt;/blockquote&gt;&lt;/p&gt;
&lt;pre&gt;
$file = &quot;file.txt&quot;;

open(IN,&quot;$file.txt&quot;) or die &quot;Cannot open $file: $!&quot;;
@data=&amp;lt;IN&amp;gt;;
close(IN);

#Now each line is an element in the array @data

$newinput = &quot;rob\n&quot;;
unshift @data, $newinput;

#now $newinput is @data[0]

open(IN,&quot;&amp;gt;$file&quot;)or die &quot;Cannot open $file: $!&quot;;
print IN @data;
close(IN);
[/code]

This will open the file, put the current data in it into an array.  Than use the unshift function to add the new input (in this case, $newinput) into the @data array as the first element.  When printed back into the file, the $newinput will be first.

Hope that helps at all.

------------------
&lt;strong&gt;A can of SPAM is opened every 4 seconds.&lt;/strong&gt; &lt;/pre&gt;</description>
     <pubDate>Tue, 21 Mar 2000 20:04:00 +0000</pubDate>
 <dc:creator>Rob Pengelly</dc:creator>
 <guid isPermaLink="false">comment 1004426 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-top-file#comment-1004425</link>
    <description> &lt;p&gt;No, it&#039;s not going to be in an array. It will probably use the push() command. I looked up the push() command in Programming Perl, and it gave a really vague example:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;code:&lt;/p&gt;
&lt;pre&gt;for(; &lt;img src=&quot;http://www.webmaster-forums.com/ubb/wink.gif&quot; alt=&quot;&quot; class=&quot;bb-image&quot; /&gt; {
push @ARRAY, shift @ARRAY;
...
}[/code]

Does anyone have a snippet of code for what I want to do?

------------------
&lt;strong&gt;Flame Hosting :&lt;/strong&gt; &lt;a href=&quot;http://www.flamehosting.com&quot; class=&quot;bb-url&quot;&gt;http://www.flamehosting.com&lt;/a&gt;
&lt;strong&gt;NT and UNIX Hosting, Dedicated Servers, and Co-Location Programs&lt;/strong&gt;

[This message has been edited by Justin Stayton (edited 21 March 2000).] &lt;/pre&gt;&lt;/blockquote&gt;</description>
     <pubDate>Tue, 21 Mar 2000 18:22:00 +0000</pubDate>
 <dc:creator>Justin S</dc:creator>
 <guid isPermaLink="false">comment 1004425 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-top-file#comment-1004424</link>
    <description> &lt;p&gt;$array[0] will print the top line of the file if your reading it from an array.&lt;/p&gt;
&lt;p&gt;If you wnt to move the rest of the file down you need to use the push() command.&lt;/p&gt;
 </description>
     <pubDate>Tue, 21 Mar 2000 18:12:00 +0000</pubDate>
 <dc:creator>Orpheus</dc:creator>
 <guid isPermaLink="false">comment 1004424 at https://www.webmaster-forums.net</guid>
  </item>
  </channel>
</rss>
