There are references to many excellent sources in Getting Started with Perl but with so much information it can be difficult to find the tidbit you need sometimes.

Another excellent way to learn is by experiment and test. With practice your testing will become more thorough and your confidence in your own observations will grow accordingly.

In this case, I don't know that a better library would suffice. I think you have found a bug in perl. Capture buffers says:

The numbered match variables ($1, $2, $3, etc.) and the related punctuation set ($+, $&, "$`", "$'", and $^N) are all dynamically scoped until the end of the enclosing block or until the next successful match, whichever comes first. (See "Compound Statements" in perlsyn.)

According to this, I would expect the numbered match variables to go out of scope at the end of each iteration of the loop block and each new iteration to begin without them set, unless they were set in an outer context. But this is not what happens.

The behavior is quite different if a continue BLOCK is added to the loop. Consider the following:

use strict; #use warnings; my @array = ('<Band:3>40M <Call:5>KD4RR <QSL_Rcvd:1>Y <QSL_Sent:1>Y', '<Band:3>40M <Call:5>K7RRR <QSL_Sent:1>Y', '<Band:3>40M <Call:5>W7FAL <QSL_Rcvd:1>Y <QSL_Sent:1>Y'); foreach (@array) { if(/<QSL_Rcvd:\d+>(Y)/i) { print "Good Record: $_\n"; } else { print "Bad Record: $_\n"; } print "in loop: \$1 = $1\n"; } continue { my $var = 1; print "in continue: \$1 = $1\n"; } print "after loop: \$1 = $1\n";

Which produces

Good Record: <Band:3>40M <Call:5>KD4RR <QSL_Rcvd:1>Y <QSL_Sent:1>Y in loop: $1 = Y in continue: $1 = Bad Record: <Band:3>40M <Call:5>K7RRR <QSL_Sent:1>Y in loop: $1 = in continue: $1 = Good Record: <Band:3>40M <Call:5>W7FAL <QSL_Rcvd:1>Y <QSL_Sent:1>Y in loop: $1 = Y in continue: $1 = after loop: $1 =

With the continue block present, $1 is not set in the second iteration of the loop. One might argue that without the continue block the foreach statement doesn't leave its block until after the last iteration, but I am not aware of any documentation that says so and I find this behavior to be quite surprising. It seems obvious that the block is entered, executed and exited once with each iteration, whether there is a continue block present or not. Yet this is not the behavior, at least in perl 5.10.0

bug report


In reply to Re^3: Problem with regexp or saved match variable. by ig
in thread Problem with regexp or saved match variable. by steve077

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.