2011/1/28 Alberto Simões <albie@alfarrabio.di.uminho.pt>
> Consider
>
> > #!/usr/bin/perl
> > use 5.012;
> >
> > my $foo = { foo => 1 };
> >
> > given($foo) {
> > when (/oo/) { say "OK" }
> > default { say "NOK" }
> > }
> >
> > say "OK" if $foo ~~ /oo/;
> > say "OK" if /oo/ ~~ $foo;
>
> I get
> NOK
> OK
> OK
>
> but I was expecting the first given/when clause to match.
>
>
From perlsyn,
Most of the time, when(EXPR) is treated as an implicit smart match of $_ ,
i.e. $_ ~~ EXPR. [...] But when EXPR is one of the below exceptional cases,
it is used directly as a boolean: [...] a regular expression match, i.e.
/REGEX/ or $foo =~ /REGEX/ , or a negated regular expression match (!/REGEX/
or $foo !~ /REGEX/ ).
when (qr/oo/) will do the trick.
Thread Previous