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

Here's the code first off: (any variables not shown are global and declared at the begnning of the script)
# __/AddStatus\____________ #| #| sub AddStatus { my @tempfile_a; my $to_add; $to_add = param('txt_status') . "\n"; my $whichfile; my $breaksread; $breaksread = 1; my $linenum; $linenum = 0; if ($chosen > $opnum) { $chosen = $chosen - $opnum; $whichfile = "closed.dat"; } else { $whichfile = "open.dat"; } open (CASEFILE, $whichfile) or dienice ("Can't open file $whichfile" +); @readfile_a = <CASEFILE>; close(CASEFILE); foreach $line (@readfile_a) { #see if we're on a break $testline = substr($line, 0, 2); if ($testline eq "\$.") { #if so increase breaksread $breaksread = $breaksread + 1; } #if it's not a break then see if the line is in the entry elsif ($breaksread == $chosen) { if ($testline eq "\$e") { print "boooo $linenum - $to_add<br>\n"; @tempfile_a[$linenum] = $to_add; $linenum = $linenum + $linenum; } } @tempfile_a[$linenum] = $line; $linenum = $linenum + 1; } # $whichfile = ">b" . $whichfile; # print "$whichfile<br>\n"; print @tempfile_a; # create a lock file my $lockfile; $lockfile="lock_the_file.loc"; while (-e $lockfile) { sleep 2; } open (LOCK,">$lockfile") || die ("Cannot open lock file!\n"); close (LOCK); open (CASEFILE, ">$whichfile"); print CASEFILE @tempfile_a; close(CASEFILE); unlink($lockfile); } #| #| End AddStatus #|_________________________________
What happens is that it either selects and reads from closed.dat or open.dat and keeps that filename in $whichfile. Now it then stores the info into @readfile_a which then is read into @tempfile_a except adding a line. After this it opens the same $whichfile again for overwrite except that it does not make any changes. I print out the @tempfile_a and it shows that a change should be made but it will not overwrite the open.dat or closed.dat files. Now I tried changing the output file to ">bb$whichfile" and it creats bbopen.dat and everytime I update it, it will create a new file fine. Then I run the script again and it also overwrites the bbopen.dat file just fine. (meaning that it creates a file and then i run the same script again to overwrite that same file) It seems to be entirely file permissions which doesn't completely make sense but at least it's being consistant. Any file that I make by rightclick->new->textdocument will not allow me to overwrite but any file generated by perl will. So it's some kind of strange permissions situation. Should be as simple as getting down to this specific server and applying permissions.

Replies are listed 'Best First'.
Re: Overwrite a file (delete a file 2. Revenge of the Sith)
by shmem (Chancellor) on Oct 11, 2006 at 17:25 UTC

    Add a die or dienice to

    open (CASEFILE, ">$whichfile");

    and try again... ah, and use strict; use warnings; ;-)

    Really, you should check the return value for every open and close.

    # better: open CASEFILE, '>', $whichfile or die "Can't write '$whichfile': $!\ +n";

    Does the perl script run under your user-id?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      All fixed. A problem with permissions. I'm also going back to add the dienice. Forgot that I had taken it out.
Re: Overwrite a file (delete a file 2. Revenge of the Sith)
by ysth (Canon) on Oct 11, 2006 at 17:26 UTC
    This line is very strange: $linenum = $linenum + $linenum;; why are you doing that?

    You might consider Tie::File, or a real database.

      lol yeah old glitch from copying and pasting a portion of function from another script. Fixed it in the other file but forgot to in this one.
Re: Overwrite a file (delete a file 2. Revenge of the Sith)
by blue_cowdawg (Monsignor) on Oct 11, 2006 at 17:21 UTC
        Should be as simple as getting down to this specific server and applying permissions.

    What's the environment that you are running under?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg