Hello All!

I've poked around a good bit, and futzed a lot with this, to no avail, so..... here's the question.

Looking at this node, i'm told that it's pretty easy to add a line to the beginning of a file:

Here's another common problem which is trivially solved by Tie::File. How do I add a new record at the beginning of a file instead of at the end? unshift @lines, $new; # Or add more than one record
Pretty much the same thing is said in the Tie::File pod

My problem is that my code (below the readmore) appears to:

  1. Overwrite the first line of the target file, rather than insert a new first line.
  2. Overwrite part of the next (second) line if the text being inserted is longer than the previous first line (the one being overwritten)

What do i want? I want to have the line in question inserted at the beginning of the file, as described in the quote/outtake above. It seems obvious that i'm doing something wrong, but i can't figure out what. I've pasted the offending sub below (yes, the script is running with -w), as well as some the STDERR output.

Thanks greatly for your attention!

#################### sub update_threads() { my $parent = $_[0]; my $msg_id = $_[1]; #check if this is a top-level, and set $parent accordingly if ($parent eq 'top') { $parent = $msg_id } my $main_threads_file = $htdocs . $path_info . "/threads.html"; my $archive_threads_file = $htdocs . $path_info . "/posts/" . $date . +"threads.html"; my $post_location = $path_info . "/posts/" . $date . "/" . $parent . " +_" . $msg_id . ".html"; my $post_name = $parent . "_" . $msg_id; my @threads; my @archive; my $link = "<li> <!-- mid=\"$post_name\" --> <a href=\"$post_location\ +">" . $params{"subject"} . "</a> by " . $params{"name"} . " <font si +ze=\"-1\">on " . $time . "</font>"; tie @threads, 'Tie::File', $main_threads_file or syslog('err', "update +_threads couldn't open file: $main_threads_file"); if ($parent == $msg_id) { unshift @threads, $link; } untie @threads; return; } ## end sub update_threads() #***************************
the STDERR Below (can i nest readmore tags? hmmm) is the full output to the STDERR during one run... i know much of it is irrelevant, and i strongly suspect that the Tie::File errors are irrelevant too, but i'm not sure...

anyhow,

main::get_date() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 50. main::read_config() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 53. main::print_postform() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 63. main::read_mcount() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 90. main::get_date() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 92. main::generate_post() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 95. main::update_threads() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 98. main::get_date() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 50. main::read_config() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 53. main::print_postform() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 63. main::read_mcount() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 90. main::get_date() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 92. main::generate_post() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 95. main::update_threads() called too early to check prototype at /usr/local/apache/cgi-bin/bthreads-perl line 98. Use of uninitialized value in read at /usr/lib/perl5/5.6.1/Tie/File.pm line 699. Use of uninitialized value in read at /usr/lib/perl5/5.6.1/Tie/File.pm line 699. Use of uninitialized value in read at /usr/lib/perl5/5.6.1/Tie/File.pm line 699. Use of uninitialized value in read at /usr/lib/perl5/5.6.1/Tie/File.pm line 699. Use of uninitialized value in read at /usr/lib/perl5/5.6.1/Tie/File.pm line 699. Use of uninitialized value in read at /usr/lib/perl5/5.6.1/Tie/File.pm line 699. Use of uninitialized value in read at /usr/lib/perl5/5.6.1/Tie/File.pm line 699. Use of uninitialized value in read at /usr/lib/perl5/5.6.1/Tie/File.pm line 699. Use of uninitialized value in read at ## and so on

update (broquaint): changed <pre> tags to <code> and dropped a chunk of repeated error messages


In reply to Tie::File strangeness with unshift by u914

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.