Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Thanks, That's a good idea :) I just cannot figure out the zero bytes bug... I've managed to stump quite a few people with it, by the way. But I think it has something to do with sysopen and truncate but I can't be sure.


I totally recoded the old sysopen part but I'm not sure if what I did will work...
sub safe_open { my $fn = shift; my $fh = do { local *FH }; my ($mode, $result); if ($fn =~ /^>[^>]/) { $mode = 1; } elsif ($fn =~ /^>>/) { $mode = 2; } else { $mode = 0; } if ($mode == 1 and $flock_enabled) { # Write mode with flock. $truncate_please = 1; my $fhtemp = do { local *FHT }; $result = open( $fhtemp, "+>>$fn" ); if ($result) { $result = open($filehandle, ">&$fhtemp" ); close($fhtemp); } } elsif ($mode == 1) { # Write mode without flock. $result = open($fh, ">$fn"); } elsif ($mode == 2) { # Append mode. $result = open($fh, ">>$fn"); } elsif ($mode == 0) { $result = open($fh, $fn); } unless ($result) { # Open failed. fatal_error("Couldn't open $fn."); } flock($fh, 2) if $flock_enabled; if ($truncate_please) { truncate($fh, 0) or fatal_error("Could not truncate file $fn." +); } return $fh; }

In reply to Re: Safe way to open files by L0rdPhi1
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 cooling their heels in the Monastery: (8)
As of 2021-06-15 13:20 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (72 votes). Check out past polls.