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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.