Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Safe way to open files by vladb
in thread Safe way to open files by L0rdPhi1

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-06-20 22:32 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (96 votes). Check out past polls.