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?
Thanks!