Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

given-when with compiled regular expression returned from method confusing behavior

by whakka (Hermit)
on Dec 12, 2009 at 22:23 UTC ( #812546=perlquestion: print w/replies, xml ) Need Help??
whakka has asked for the wisdom of the Perl Monks concerning the following question:

I just got bit expecting a different behavior from an object that holds compiled regex's (qr//'s) and accessing one of those in a given-when switch statement.

Some test code:

package RE::Object; sub new { bless { m1 => qr/value is (\d+)$/ }, 'RE::Object'; } sub m1 { shift->{m1} } package main; use strict; use warnings; use 5.010; use Test::More 'no_plan'; my $re = RE::Object->new(); my @lines = ( 'dummy', 'value is 42' ); for my $line ( @lines ) { given ( $line ) { when ( $re->m1 ) { # *Update* always evaluates method calls as + a boolean (JadeNB) is( $1, undef, 'bit me' ); continue; } when ( do { $re->m1 } ) { is( $1, 42, 'do' ); continue; } when ( $_ ~~ $re->m1 ) { is( $1, 42, 'explicit' ); } default { is( $line, 'dummy', 'dummy line' ); } } }
The output is:
ok 1 - bit me ok 2 - dummy line ok 3 - bit me ok 4 - do ok 5 - explicit 1..5

So without an explicit do block or explicitly smart-matching $_ the bare method automatically matches any line. Can anyone explain this behavior?


Replies are listed 'Best First'.
Re: given-when with compiled regular expression returned from method confusing behavior
by JadeNB (Chaplain) on Dec 12, 2009 at 22:42 UTC

    From Switch statements:

    Most of the time, when(EXPR) is treated as an implicit smart match of $_, i.e. $_ ~~ EXPR. (See "Smart matching in detail" for more information on smart matching.) But when EXPR is one of the below exceptional cases, it is used directly as a boolean:
    • a subroutine or method call
    Since $re->m1 is true, when ( $re->m1 ) succeeds.

    UPDATE: I just noticed that the elided list is followed by

    In those cases the value of EXPR is used directly as a boolean.
    … in case you didn't believe it the first time, I guess. :-)

      Oh shoot somehow my eyes skipped over that part, thanks.

      Update: Harsh! The stress of hobby-coding when I have two finals next week are getting to me, I should really just do this when I have the time.

      Rejected witty excuses:

      • I guess you have to be smart to use smart-matching!
      • It seems the part where it says "These rules look complicated, but usually they will do what you want" doesn't really mean " go ahead and ignore the documentation."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://812546]
Approved by almut
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2018-06-22 15:51 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.