I've made a big program in perl, and i've isolated the problem to the following procedure.

Basicly what I want is to add the array given as a parameter, as a new column in $outfile
When i do the print `cat $outfile`, it correctly displys the file before I edit it.

After I've open and closed the file I check again with the print `cat $outfile`. And I can see that the file has been updated.

Now the next time I call insert, It for some strange reason opens the oldfile since the print `cat $outfile`. Displays the values like the first time I called the print `cat $outfile.

This is indeed the weirding way of perl

sub insert{ print "insert called:". $ins ."\n"; $ins++; my @toBeInserted = @_; print `cat $outfile`; open(FILE2,$outfile)||die "you stupid ****, file doesn't exit\n";; open(FILE3,">"."tmp.file")||die "you stupid ****, file doesn't exit\ +n";; my $cnt=0; while (<FILE2>){ chomp; my @fields = (split /[ \t]+/,$_); push(@fields,$toBeInserted[$cnt]); foreach(@fields){ my $tmpString = $_ . "\t"; print FILE3 $tmpString; } print FILE3 "\n"; $cnt++; } close(FILE2); close(FILE3); #now swap the files unlink($outfile) || die "Cannont delete old file.\n"; rename("tmp.file", $outfile) || die "Cannot rename file\n"; print `cat $outfile`; }
thanks in advance

In reply to weird file write problem by monkeyking

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.