My only point was regarding when you simply want to return a false value to indicate a subroutine failure. Consider the following contrived example where we only want to process strings beginning with a particular pattern:

#!/usr/bin/perl -w use strict; my @patterns = (qr/^foo/, qr/^qux/); my @strings = ('foo bar', 'bar bar', 'qux bar'); my @stuff; foreach my $string (@strings){ if(@stuff = dice_it($string)){ print "Processing: $string\n"; process_stuff(@stuff); } } sub dice_it { my $string = shift; foreach my $pat (@patterns) { return split //, $string if $string =~ /$pat/; } return undef; } sub process_stuff { foreach (@_) { print "<$_>"; } print "\n"; }

I'm not suggesting this a terribly common problem (or that the above is a good way to approach this particular example). I just wanted to point out that returning 'undef' as a failure mode isn't always appropriate -- and people who do so may forget that an array containing one undefined element still evaluates to true so they may bang their head for a while before they realize why they are processing the string 'bar bar' and getting a warning. Changing the last line of dice_it() to just a bare 'return' statement alleviates the problem because it returns the 'right' thing depending on context.


In reply to Re: Re:(2) Parse Loops with flat text files. (code) by danger
in thread Parse Loops with flat text files. (code) by deprecated

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.