Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Question on style/technique

by synapse0 (Pilgrim)
on Jul 06, 2001 at 04:29 UTC ( [id://94353]=perlquestion: print w/replies, xml ) Need Help??

synapse0 has asked for the wisdom of the Perl Monks concerning the following question:

There was a post recently that had a mentioning for style for open().. Often, when using open in a place where it's ok to fail the open attempt i use:
if (open(FH, "< $file") { do fun stuff with file close(FH); } else { log error to some log file possibly display error message } continue on ..
Now, most of everything i see uses a die for open failure.. I know there are other things you can do as well, my question is, in the world of Real Perl Programming(TM) how horribly terrible is it to use an if else for a file open like that? what is the most elegant way to do that, if you don't want to exit on a failure? Thanks for the info,


Replies are listed 'Best First'.
Re: Question on style/technique
by VSarkiss (Monsignor) on Jul 06, 2001 at 05:47 UTC
    The big question is: Can your program run despite the file open failing? If, for example, you're writing something that digests a log file, and you fail opening the log file, give up the ghost. There's no point in trying to continue. OTOH, if your program can limp along without the file, then just issue a warning and continue. (Continuing the same "digest-a-log-file" example, say you can't open the password file to map UIDs to login names; you can process the log file, although you probably want to tell someone you couldn't open the file. (Contrived example, you get the idea.))

    The next question is, what does "issue a warning" mean? That depends very much on the programming shop in which you're running, and whom you want to alert. Don't think in terms of "most elegant", think in terms of "most useful". The appropriate thing may be to write a record in a log file, send an email, or even use CGI::Carp qw(fatalsToBrowser).

    Your question also involved using if/else to handle file open errors. This really goes to some programming style issues. I personally despise style guidelines that assert that an if should always "exit at the bottom". To me, code structured like this is a nightmare:

    if (open(FH, $file)) { ...complicated logic extending many many lines.... close(FH); } else { warn "Couldn't open $file: $!"\n"; }
    because after comprehending the complicated logic in the first block, I have to pop my mental stack and say, "Now who the heck does this else belong to?"

    To me, the else block is part of the file opening logic and should be kept together. That is, the code should do something more like this:

    unless (open(FH, $file)) { ...send email, sound the klaxons, raise a ruckus, etc. ...if I really can't continue, I'll die or exit here. } else { fun stuff with FH }
    The key point about style, IMHO, is to keep all the file open handling logic together.


      There are of course many programs that you do not want to issue any warnings if they cannot open certain files. Lots of application read in various, optional, configuration files, one in the current directory, one in the users home directory, and a system wide config file. Not all config files will exist, or are readable. You want the application to silently skip them. This is very acceptable code:
      foreach my $file (@files) { if (open my $fh => $file) { read in file do whatever you need to do } }

      -- Abigail

        Thanks for the replies everyone, Abigail hit on the mark what i am doing with my code. I often have instances where i just need to grab a bit of data from a file, or append a string, something that can quickly be done in a block and filehandle closed, and something that doesn't necessarily need to happen.. I just didn't know if this might be percieved as "bad coding" practice, since i've never seen it done like that in other's examples.
        Once again, thanks...

Re: q. on style/technique
by damian1301 (Curate) on Jul 06, 2001 at 04:56 UTC
    Well, I would say you have these options to trap the error:
    1. eval it and check for errors in $@
    2. use an if/else statement as above.
    3. use a little open B,"$blah" or error_subroutine("$!")#and do stuff in the routine.
    The thing is, if you do not open the file successfully and do not write/read the data you want from/to the file correctly, it could (and most likely will) cause some problems down the road.

    If you are using -w or warnings as you should, you might get those annoying little Use of uninitialized value errors which could keep your program from working at all.

    The advice I would give you is, just die if something goes wrong because your program just might die later (unless you clog your program with double-checks everything). So, I would use this:
    unless(open FH,"$file"){ open(LOG,">>$log") or die"Error writing to error-log we +ird: $!"; print "Error at ",__LINE__," here's what happened: $!"; close LOG; }else{ proceed as normal.... }
    UPDATE: Thank you to crazyinsomniac for pointing out that open FH, $file || or die; will always result as true and hence never fail. Three cheers for my hero! ;)

    $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
Re: Question on style/technique
by John M. Dlugosz (Monsignor) on Jul 06, 2001 at 07:23 UTC
    I'd say that there is nothing wrong with the if/else, if you indeed want to do different things based on the file opening or not.

    The or die is just what you should always do if you don't otherwise check the result in your logic. It's the default, fallback thing to do if you don't have anything else to do.

    Hmm, maybe Perl 6 will have a property for a sub that will warn if it's called in void context, pointing out that "you better check the result of this!".


Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-16 07:47 GMT
Find Nodes?
    Voting Booth?

    No recent polls found