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!


In reply to given-when with compiled regular expression returned from method confusing behavior by whakka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.