develooper Front page | perl.perl5.porters | Postings from January 2011

Re: Given/when strangeness

Thread Previous
From:
Eric Brine
Date:
January 28, 2011 10:52
Subject:
Re: Given/when strangeness
Message ID:
AANLkTinYkvqyEPL_mU_SV6SNYqUaa+HK1Jw+Mc-+2qkD@mail.gmail.com
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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About