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

Re: Given/when strangeness

Thread Previous | Thread Next
From:
Alberto Simões
Date:
January 28, 2011 10:54
Subject:
Re: Given/when strangeness
Message ID:
4D4310F5.2040805@alfarrabio.di.uminho.pt
Thank you, Leon and Eric.

I knew I missed something :)
Cheers

On 28/01/2011 18:53, Leon Timmermans wrote:
> 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.
>>
>> Is this a bug or I missed some piece of documentation?
>
> You missed a piece of documentation «when (/regex/)» is not equivalent
> to «$_ ~~ /regex/» but «$_ =~ /regex/». This is documented in
> perlsyn#Switch-statements as one of the special cases.
>
> I don't know why it works like that though. I'm kind of curious about that.
>
> Leon

-- 
Alberto Simões

Thread Previous | Thread Next


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