While slimming down some code in a parser, I ran across what I thought was a good solution only to create a nasty debugging situation. Here's a code snippet:
sub dis { shift =~ /.?.?(.)/; }
With the idea of stripping off the optional first two characters and returning the last character. For instance dis('QNP') returns 'P' and so on. To my surprise, this worked---at least seemed to at first. It turns out that in some cases this works, but in others, there are strange side-effects that that leave you looking at once working code that perl now claims is defective. Lucky for me, I typically change only one thing at a time before regression testing and it was fairly easy to spot this one. Here is the patched code:
sub dis { shift =~ /.?.?(.)/; $1; }
The reason I labeled this node a puzzle is that I've no clue as to what was wrong or why this fixes it! So I ask the more knowledgeable out there, what is going on here?

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

In reply to Parse Puzzle by hsmyers

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.