Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Think about Loose Coupling
 
PerlMonks  

perl 5.10 bug or not?

by sotona (Acolyte)
on Aug 16, 2013 at 16:00 UTC ( #1049755=perlquestion: print w/ replies, xml ) Need Help??
sotona has asked for the wisdom of the Perl Monks concerning the following question:

my $email = 'someuser@example.com' $email =~ /(.*) (<.*>)/; say "1='$1' 2='$2'";

produces 1='example.com' 2='', but it should give 1='' 2=''. Are there any ideas how to work around this in a right way? Upgrading to newer Perl versions are not an option at the moment.

Comment on perl 5.10 bug or not?
Download Code
Re: perl 5.10 bug or not?
by davido (Archbishop) on Aug 16, 2013 at 16:04 UTC

    Is it possible you had an earlier successful match that set $1 and $2?

    Provide the actual code. What you posted won't result in a match at all (it will, however, produce a syntax error. ;) If we're going to look for a bug in Perl 5.10 we need to make sure that the example code (1) runs, and (2) stands alone as an example that will replicate the problem.


    Dave

Re: perl 5.10 bug or not?
by SuicideJunkie (Priest) on Aug 16, 2013 at 16:06 UTC

    You aren't testing to see if the match succeeded.

    The values of $1 and $2 are meaningless unless the match succeeded.

Re: perl 5.10 bug or not?
by AnomalousMonk (Monsignor) on Aug 16, 2013 at 17:59 UTC

    An example of the point made by davido and SuicideJunkie. Note that in the first print statement, which is not preceded by any successful match, the output is as you expect.

    >perl -wMstrict -lE "my $email = 'someuser@example.com'; $email =~ /(.*) (<.*>)/; say qq{A: 1='$1' 2='$2'}; ;; $email =~ /([^@]+\z)/; say qq{B: 1='$1' 2='$2'}; ;; $email =~ /(.*) (<.*>)/; say qq{C: 1='$1' 2='$2'}; " Use of uninitialized value $1 in concatenation (.) or string at -e lin +e 1. Use of uninitialized value $2 in concatenation (.) or string at -e lin +e 1. A: 1='' 2='' Use of uninitialized value $2 in concatenation (.) or string at -e lin +e 1. B: 1='example.com' 2='' Use of uninitialized value $2 in concatenation (.) or string at -e lin +e 1. C: 1='example.com' 2=''
Re: perl 5.10 bug or not?
by ww (Bishop) on Aug 16, 2013 at 20:32 UTC
    ... and, also note that your first capture element -- the dreaded dot-star (which is anything, any number of times including zero)-- is greedy, 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.

    Regexen can, of course, be "greedy" without dot-star. Greedy means the regex element -- in this case, using a star/asterisk quantifier -- will match as much data as possible until a new line (or other construct not present in your regex) shuts them down.

    Super Search this site (or use big G) for "greed" and/or "greedy" for examples.

    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.

      "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
        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 = 'someuser@example.com'

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1049755]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-04-19 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls