Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^2: perl 5.10 bug or not?

by tobyink (Abbot)
on Aug 16, 2013 at 22:09 UTC ( #1049804=note: print w/replies, xml ) Need Help??

in reply to Re: perl 5.10 bug or not?
in thread perl 5.10 bug or not?

"so the second match would never occur, even if the code compiled because the first match would have swallowed everying in your (then-current) line of data."

Not so!

use v5.10; my $email = 'foo <bar>'; $email =~ /(.*) (<.*>)/; say "1='$1' 2='$2'";

Greedy matches don't automatically swallow everything. A quote from perlre, my emphasis:

"By default, a quantified subpattern is "greedy", that is, it will match as many times as possible (given a particular starting location) while still allowing the rest of the pattern to match."

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^3: perl 5.10 bug or not?
by ww (Bishop) on Aug 16, 2013 at 23:57 UTC
    True... (and probably I should give myself a downvote for lack of precision)... BUT , there are no GT or LT symbols in OP's sample
    my $email = ''

    hence, dot star swallows all, in the relevant example.

    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.
      ... there are no GT or LT symbols in OP's sample
          my $email = ''
      hence, dot star swallows all, in the relevant example.

      If the relevant example is
          my $email = ''
          $email =~ /(.*) (<.*>)/;
          say "1='$1' 2='$2'";
      from the OP, then dot star – i.e., (.*) – swallows nothing at all. The  '<' and  '>' characters are required to match, therefore there is no match, no swallowing, no capturing, nuttin'. As pointed out elsewhere, not even $1, $2, etc., are altered. In fact, because the first  (.*) capture group is followed by a required  ' ' (space), the angle brackets don't even figure in:  .* will initially grab everything, then backtrack repeatedly to try to match a space; finding none, it will eventually give up everything it grabbed and announce its miserable failure, triggering overall match failure.

      The problem is actually why do I get some capture if I had no previous successful match.

      In case of "foo <bar>" I definitely should get two captures, but I have no clue why do I get a capture "" with "" sample. Observed on v5.10.0 DEVEL34916

        See perlre:

        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.

        $1 and $2 have their old values from a match somewhere else in your code.

        Always guard use of $1 with an if statement to make sure it matched:

        if( m/(.*) <(.*)>/ ) { print "Matched '$1' + '$2'"; } else { print "Did not match"; };

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049804]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-01-22 10:34 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (187 votes). Check out past polls.