Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

File open issue

by Anonymous Monk
on Dec 19, 2012 at 18:49 UTC ( #1009601=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am debugging a script and found on this part of the code where it is assigning a variable if the file can't be opened, if I use "die" the code dies, but without using "die" it does what it supposed to do but I get a warning message. My question is if there is a better way to do this, because I need to use the "die" on the "open" file code.
Here is the peace of the code I am talking about.
my $nofile; my $tempfile = "$tempdir/$ENV{'REMOTE_ADDR'}"; open (TMPFILE, "$tempfile")|| ($nofile = 1); my (@tmpfile) = <TMPFILE>; close TMPFILE;

Thanks you!

Comment on File open issue
Download Code
Re: File open issue
by davido (Archbishop) on Dec 19, 2012 at 19:09 UTC

    What is the warning that you get? And when you use  or die $!;, what is the output?


    Dave

      What about this:
      if(-r $tempfile) { open (TMPFILE, "$tempfile") or die "Can't open $tempfile: $!"; (@tmpfile) = <TMPFILE>; close TMPFILE; }else { $nofile = "1"; }
Re: File open issue
by MidLifeXis (Prior) on Dec 19, 2012 at 19:43 UTC

    The AM has one reasonable solution, but it assumes that the only failure mode that the $nofile variable applies to is the case where the file is not readable at the time it is tested.

    • There is a race condition between the test and the open call, and
    • the file may fail to open for other reasons.

    If you can live with those conditions, great. Otherwise, another solution (similar to yours) might be:

    my $nofile; my $reason; my $tempfile = "$tempdir/$ENV{'REMOTE_ADDR'}"; my @tmpfile; if ( open( my $temp_fh, "<", $tempfile ) ) { @tmpfile = <$temp_fh>; close( $temp_fh ); # You don't need this, $temp_fh should be limit +ed in scope } else { $nofile = 1; $reason = $!; # update: original typo == $@ }

    --MidLifeXis

Re: File open issue
by jwkrahn (Monsignor) on Dec 20, 2012 at 04:13 UTC
    my $tempfile = "$tempdir/$ENV{'REMOTE_ADDR'}";

    Quotes on a bareword hash key are redundant.

    open (TMPFILE, "$tempfile")|| ($nofile = 1);

    You shouldn't quote variables: What's wrong with always quoting "$vars"?    You are adding extra work for perl which has to interpolate the variable and then copy its contents to the string.

    Because open returns true on success or false on failure you could simply do:

    my $nofile = open TMPFILE, $tempfile;
    my (@tmpfile) = <TMPFILE>;

    The parentheses are superfluous because an array already provides list context.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009601]
Approved by Corion
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2014-08-20 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (123 votes), past polls