monkeyking has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: weird file write problem
by GrandFather (Saint) on Mar 29, 2007 at 02:38 UTC

    If I put some driver code in front of your sample and remove the cat calls (which are sub optimum on Windows) I get the following:

    use strict; use warnings; my @inserts = ( [1, 1, 1], [2, 2, 2] ); my $outfile = 'delme.txt'; my $ins = 0; open OUTFILE, '>', $outfile; print OUTFILE <<DATA; first second third 1 2 3 x y z DATA close OUTFILE; insert (@$_) for @inserts; @ARGV = $outfile; $/ = undef; print <>; # OP's sub sans cat calls follows

    Prints:

    insert called:0 insert called:1 first second third 1 2 1 2 3 1 2 x y z 1 2

    Where's the problem?


    DWIM is Perl's answer to Gödel
      hmm, I don't really understand this, but I took your program
      And simply changed you inserts, so that it is two arrays instead.
      #!/usr/bin/perl -w use strict; use warnings; my $ins=0; my @insert1 = qw(1 1 1); my @insert2 = qw(2 2 2); my $outfile = 'delme.txt'; open OUTFILE, '>', $outfile; print OUTFILE <<DATA; first second third 1 2 3 x y z DATA close OUTFILE; insert (@insert1); @ARGV = $outfile; $/ = undef; print <>; insert (@insert2); @ARGV = $outfile; $/ = undef; print <>; sub insert{ print "insert called:". $ins ."\n"; $ins++; my @toBeInserted = @_; open(FILE2,$outfile)||die "you stupid fuck, file doesn't exit\n";; open(FILE3,">"."tmp.file")||die "you stupid fuck, 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"; }
      And this gives me the following.
      insert called:0 first second third 1 1 2 3 1 x y z 1 insert called:1 first second third 1 1 2 3 1 x y z 1 2
      Thanks for the reply

        Consider:

        use strict; use warnings; my @insert1 = qw(1 1 1); my @insert2 = qw(2 2 2); my @inserts = (\@insert1, \@insert2); my $outfile = 'delme.txt'; my $ins = 0; open OUTFILE, '>', $outfile; print OUTFILE <<DATA; first second third 1 2 3 x y z DATA close OUTFILE; for (@inserts) { insert (@$_); local @ARGV = $outfile; local $/ = undef; print <>; } ...

        Prints:

        insert called:0 first second third 1 1 2 3 1 x y z 1 insert called:1 first second third 1 2 1 2 3 1 2 x y z 1 2

        Note that the bogus result from your reworking of my code was due to $/ being set to undef and therefore the <FILE2> seeing the entire file as one line on the second iteration. Sometimes I include "interesting" ways of doing stuff to make you explore the language and the documentation a little. In this case read perlvar to gain a little insight. ;)


        DWIM is Perl's answer to Gödel