[Israel.pm] bitten by context

Gaal Yahas gaal at forum2.org
Tue Apr 24 05:01:45 PDT 2012


You're being bitten by something else: precedence (in ? :).

This works:

my $has_red_2 = (grep /red/, @l) ? 1 : 0;


On Tue, Apr 24, 2012 at 2:54 PM, ynon perek <ynonperek at gmail.com> wrote:

> Hi Everyone,
> I was bitten today by this snippet that I still can't understand (second
> test fails):
>
> use v5.14;
> use Test::More;
>
> my @l = qw/red blue green/;
>
> my $has_red_1 =   grep /red/, @l;
> my $has_red_2 = ( grep /red/, @l ? 1 : 0 );
>
> is ( $has_red_1, 1, "Expected" );
> is ( $has_red_2, 1, "This one fails - Why ?" );
>
> done_testing;
>
> I know that grep does nothing in void context, but here I have a valid
> boolean context - what's going on ?
>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20120424/2a17094f/attachment.htm 


More information about the Perl mailing list