As another example of unhelpful behavior (similar to No Pause on Elsif in Debugger), here's some code I was modifying today:
############################### # try to sort fields as numbers first, then as strings # keep going until there's a difference sub smart_sort { my @a = split /[\s_]+/, lc($a); my @b = split /[\s_]+/, lc($b); for my $i ( 0..(@a < @b ? $#a : $#b) ) { if ( $a[$i] =~ $NUMBER_ONLY_REGEX # line 214 and $b[$i] =~ $NUMBER_ONLY_REGEX and $a[$i] <=> $b[$i] ) { return $a[$i] <=> $b[$i]; } elsif ( ( my @ai = /^(\d+)(.+)/ ) # error/bug is here and ( my @bi = /^(\d+)(.+)/ ) ) { if ( $ai[0] <=> $bi[0] ) { return $ai[0] <=> $bi[0]; } elsif ( $ai[1] cmp $bi[1] ) { return $ai[1] cmp $bi[1]; } } elsif( $a[$i] cmp $b[$i] ) { return $a[$i] cmp $b[$i]; } } return $a cmp $b; }
gives the error:
Use of uninitialized value in pattern match (m//) at /user/sgriffit/perl/mergen.pl line 214.
Line 214 is only the start of the if/elsif/... chain. Not too helpful. Glad there's only 1 place where a match is happening.

Now I realize there are several things there that could be changed to improve the error reporting. For instance, since most of the blocks return, the elsifs could be ifs instead.

I don't really want to spend time thinking "If this code has a bug, what's the best way of writing it so it's easy to spot?" Perhaps I'm flogging a dead horse...

Here's the cleaned up version, with more comments:

sub smart_sort { my @a = lc($a) =~ /(\d+|[a-z]+)/ig; # better split on fields my @b = lc($b) =~ /(\d+|[a-z]+)/ig; for my $i ( 0..(@a < @b ? $#a : $#b) ) { # try numbers first if ( $a[$i] =~ $NUMBER_ONLY_REGEX and $b[$i] =~ $NUMBER_ONLY_REGEX and $a[$i] <=> $b[$i] ) { return $a[$i] <=> $b[$i]; } # fallback to string compare if( $a[$i] cmp $b[$i] ) # no "elsif" { return $a[$i] cmp $b[$i]; } } # can't decide by fields, decide by whole string return $a cmp $b; }

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to No Pause on Elsif in Debugger, Part 2 by QM

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.