Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
There's nothing wrong with unless
Sure there is. And I spent a page and a half in PBP explaining precisely what was wrong with it. In summary:
  • unless is fine for simple conditionals.
  • But it makes compound conditionals harder to understand (because, in general, people are relatively poor at applying de Morgan's law correctly).
  • In particular it makes conditionals that include partial negations very much harder to comprehend.
  • Even conditionals that start out simple, don't always stay simple; extra subconditions are often identified and handled during maintenance or extension of code

The result is that (as PBP points out):

unless and until are inherently harder to maintain. In particular, whenever a negative control statement is extended to include a negative operator, it will have to be re-cast as a positive control, which requires you to change both the keyword and the conditional....Not only is that extra effort, it's error-prone effort. Reworking conditionals in that way is an excellent opportunity to introduce new and subtle bugs into your program....This kind of mistake is unfortunately common, but fortunately very easy to avoid. If you never use an unless or until, then you'll never have to rewrite it as an if or while. Moreover, if your control statements are always "positive", your code will generally be more comprehensible to all readers, regardless of the complexity of its logical expressions...

I don't proscribe unless because it's never appropriate. I recommend against it because it's often eventually inappropriate. Because, in all but the simplest cases, it ultimately makes comprehension and maintenenance harder and provides an opportunity for bugs to creep in as code is extended.

Now, you may not agree with that arguments or my conclusion, and that's fine. But it's not accurate to assert that I'm merely letting:

personal opinions slip in there

or to imply that I did not have carefully thought-out reasons for my advice.

Indeed, it's easy to demonstrate that the deprecation of unless was not just personal opinion. If you examine the source code of my modules that predate PBP, you will find many instances of unless. So, until I sat down to examine and codify Perl best practice my personal opinion was apparently that unless was fine. It was only after I reviewed the arguments for and against, discussed the issue (vigorously and at length ;-) with my many reviewers, examined the mistakes that I and many of my clients had made using unless, and thought about the long-term implications of deploying the construct, that I concluded it should not be used.

Of course, that still is a personal opinion—just like everything else that everyone else ever says—but it a personal opinion based on reasoned argument and extensive professional and pedagogical experience.

On the other hand, your subsequent point that blind acceptance of my advice is contrary to the spirit of the book is perfectly valid. I spent the first chapter asking people to think about these issues, to evaluate my arguments, and then to decide for themselves. However, not everyone has the time, the will, or the capacity to do that, which is why I tried to make the advice as careful and conservative as possible, so that those who do adopt it wholesale will be unharmed by the suggestions I made.

By the way, to answer the original poster's original question, my suggestion would be:

while (my $line = <>) { croak "I can't handle this crap! '$line'\n" if $line !~ m{\A (foo|bar) }xms; my $dumb_example = $1; .... .... }

In reply to Re^2: Regex Or Die! by TheDamian
in thread Regex Or Die! by pileofrogs

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 exploiting the Monastery: (8)
    As of 2014-09-22 15:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (198 votes), past polls