I tend to use else (in these kinds of very simple cases) when the block before it occupies much in the way of vertical space in the editor window, but often exclude it when the preceding block is brief. That way the intent is obvious even when the introductory if() clause is scrolled off of the top of the editor window. If I was doing that test, it'd be:

my $name = param('name'); if (defined $name && length $name) { # I prefer to test for the pass case rather than the fail. print "Thank you, $name, your submission is complete.\n"; } else { print "Sorry, name can't be empty.\n"; } exit;

Your two examples and mine are all functionally equivalent, so it's just a matter of how the author likes to see things done. No worries.

Edit for clarity: I also tend to minimize the use of exit and/or die whenever possible, which is why I included the else in my example. I use length in case the user's name is 0e0 or some such nonsense.


In reply to Re: Coding styles using if/else by gloryhack
in thread Coding styles using if/else by sulfericacid

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, 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:
    & &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.