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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|