Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Safe way to open files

by vladb (Vicar)
on Jun 15, 2002 at 13:58 UTC ( #174838=note: print w/replies, xml ) Need Help??


in reply to Safe way to open files

Althought, I don't have an immediate direct answer to your question, I felt that in the mean time I could suggest a few improvements to your subroutine. I'll update this node if I find a suitable explanation for your problem at hand, though.

Normally I feel it's a good idea to separate the code that reports something from the code that simply does something. Therefore, as an improvement to your sub, I would move the call the fatal_error() method (which simply prints an error string to the client) outside of your subroutine and let the caller script handle the outcome of the safe_open() method. You could also look into making your method die with a custom error message, which could be picked up by a caller script using the try { }; if ($@) { ... } construct. So, the sub would look similar to this:
# MAIN my $fn = shift; try { safe_open($fn); }; if ($@) { # here, print fatal error fatal_error($@); } # SUBS sub safe_open { my $fn = shift; my $fh = do { local *FH }; my ($mode, $result); # reset the do_trun variable. # and also, make it local to this sub.. why keep it global? ;) my $do_trun; if ($fn =~ /^>[^>]/) { $mode = 1; } elsif ($fn =~ /^>>/) { $mode = 2; } if ($mode == 1 and $flock_enabled) { # Write mode with flock. $do_trun = 1; sysopen($fh, $fn, O_CREAT | O_WRONLY) or die "Failed to open '$fn' in write mode with flock."; } elsif ($mode == 1) { # Write mode without flock. open($fh, ">$fn") or die "Failed to open '$fn' for writing (no flock)."; } elsif ($mode == 2) { # Append mode. open($fh, ">>$fn") or die "Failed to open '$fn' for appending."; } elsif (!$mode) { open($fh, $fn); or die "Failed to open '$fn'."; } if ($flock_enabled) { flock($fh, 2) or die "Failed to flock '$fn'."; } if ($do_trun) { truncate($fh, 0) or die "Could not truncate file $fn."; } return $fh; }


Update: Err.. I guess this is a classical example of how things might go wrong when you resort to using global variables. From the code, I can't quite see where you receive/get/set/modify the $do_trun variable. It might be that you still have it set to some positive (true) value on consequent calls to the method, which causes your file to be truncated. I guess, you'll simply have to reset the $do_trun variable to 0 every time inside the actual sub to eliminate the 'once in a blue moon' phenomena. ;-)

_____________________
# Under Construction

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://174838]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-06-20 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (96 votes). Check out past polls.

    Notices?