Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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 { ....do fun stuff with FH }
The key point about style, IMHO, is to keep all the file open handling logic together.

HTH


In reply to Re: Question on style/technique by VSarkiss
in thread Question on style/technique by synapse0

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2021-03-05 01:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favorite kind of desktop background is:











      Results (108 votes). Check out past polls.

      Notices?