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:
The output is: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' ); } } }
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?
Thanks!
|
|---|
| 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 | |
by whakka (Hermit) on Dec 12, 2009 at 22:46 UTC |