Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I have this problem: to capture two strings into an URL, but if the first string have the 'video' string, I need that $1 and $2 return '' (o undefined).

foreach my $url ( '<a href="/story/43480/">The Bottled Water Lie</a>', '<a href="/story/video/43480/">The Bottled Water Lie</a>', ) { print " $url\n"; if ( $url =~ m{href="(.+)">(.+)</a>} ) { print "O1: $1\n"; print "O2: $2\n"; if ( $1 !~ /video/ ) { print "Y1: $1\n"; print "Y2: $2\n"; } else { print "N1: $1\n"; print "N2: $2\n"; } print "F1: $1\n"; print "F2: $2\n"; } print "L1: $1\n"; print "L2: $2\n"; }
<a href="/story/43480/">The Bottled Water Lie</a> O1: /story/43480/ O2: The Bottled Water Lie Y1: /story/43480/ Y2: The Bottled Water Lie F1: /story/43480/ F2: The Bottled Water Lie L1: /story/43480/ L2: The Bottled Water Lie <a href="/story/video/43480/">The Bottled Water Lie</a> O1: /story/video/43480/ O2: The Bottled Water Lie N1: N2: F1: F2: L1: /story/video/43480/ L2: The Bottled Water Lie
Ok. This work. The second $url don't show nothing... but... into the else part (N1 & N2 lines) shown that match variables are reset to undef. And the F1 & F2 lines show undefined vars also.

perlre say:

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.)

NOTE: failed matches in Perl do not reset the match variables, which makes it easier to write code that tests for a series of more specific cases and remembers the best match.

But the second 'if' reset the match variables in a failed test.

So... I need a very deep explanation of this mystery, please.

Anyway, I reduced the problem to

$url =~ m{href="(.+)">(.+)</a>} and $1 !~ /video/; print "$1 $2 \n";
but I don't know how it is working, also :-(

In reply to Extraneous behaviour of match variables by explorer

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2018-06-24 21:40 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.