Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Scratching my head...

by spudzeppelin (Pilgrim)
on May 01, 2001 at 00:57 UTC ( #76746=perlquestion: print w/ replies, xml ) Need Help??
spudzeppelin has asked for the wisdom of the Perl Monks concerning the following question:

I've been scratching my head on this one for a few days now. I'm getting spurious file writing errors (as in, randomly it FAILS to write the output file) with a routine sandwiched in a much larger piece of code, that resembles:

open INFILE, "$inputfn" || die "Error reading input file: $!\n"; open OUTFILE, ">$outputfn" || die "Error opening output file: $!\n"; while (<INFILE>) { ##...a bunch of stuff munged with regexes omitted... print OUTFILE "$_\n"; } close OUTFILE; close INFILE;

This has only been happening on Linux, but on machines running two different distros, two different (albeit both 2.2.x) kernels, and both on nfs-mounted filesystems and not. It's as if the open call is dying (ie. there isn't even a zero-length file lying around) without the die() being called.

Anyone have any thoughts? Known idiosyncracies that are similar? etc.?

Spud Zeppelin * spud@spudzeppelin.com

Comment on Scratching my head...
Download Code
Re: Scratching my head...
by turnstep (Parson) on May 01, 2001 at 01:03 UTC

    Change that to:

    open INFILE, "$inputfn" or die "Error reading input file: $!\n";
    or perhaps:
    open(INFILE, "$inputfn") || die "Error reading input file: $!\n";

    The problem is the precedence of || and ",". In your code, if open fails to open $inputfn for any reason, it tries to "open" the die statement! Use parens to say what you really mean, or use the ultra-low priority "or" operator, which makes the comma act as you might expect.

    (Of course, this applies to both of your opens. When in doubt, use parenthesis around all your function calls.)

      I hope you used "or" instead of "||" in the output file, too. Otherwise, you may be inadvertently skipping the die() call.

      Update: Sorry, turnstep has explained it more completely now...

      buckaduck

      My bad, actually the existing code already has the open() || die () syntax. That's what I get for trying too hard to scrub any meaningful proprietary content from it *g*.

      So it isn't the ||s. One other thing to rule out: any kind of condition where it wouldn't be called at all -- this is main-line code in a subroutine called inside a while loop on the result set of a database query, with adjacent results both above and below it in the result set executing flawlessly. Like I said, this problem is spurious -- I can't pin down any cause (or even a predictable failure) whatsoever within the software.

      Spud Zeppelin * spud@spudzeppelin.com

        Well, if the file is not getting created, and nothing shows up on the die, and you have the parens correct (grin) then I would suggest:

        • Open is being blocked and hangin as it waits to open the file (not likely)
        • Something is erasing the file after it is written.
        • Something is intercepting your die messages.

        The first two depend on the rest of your code. The last can be checked with a quick insert of:

        my $tmperror = "/tmp/temp.error"; open(STDERR, ">$tmperror") or die "Ironic: Cannot write errors to $tmperror: $!\n";
        right before the open and see if it catches anything. Throw an error in on purpose to see if the temp file gets the error.

      This nicely illustrates the reason why I never use the 'open () || die' idiom, and I wish others wouldn't use it either. I think it suckers people into making this mistake, which in turn causes them a lot of needless debugging hell.

        What idiom do you use? I strongly prefer using "or" myself, but there is nothing wrong with using || as long as you get the parens straight. Besides, the 'debugging hell' tends to teach a very important lesson on the importance of precedence. :)

Re: Scratching my head...
by wardk (Deacon) on May 01, 2001 at 01:05 UTC

    change the "||" to or, you should see the error message.

    open OUTFILE, ">$outputfn" or die "Error opening output file: $!\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-07-13 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls