[Israel.pm] bitten by context

ynon perek ynonperek at gmail.com
Tue Apr 24 05:14:14 PDT 2012


Ah yes - precedence :)

And thanks for the deparse trick, it looks like a true time saver.

On 24 April 2012 15:05, Gabor Szabo <gabor at szabgab.com> wrote:

> 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 ?
>
> save the script in a.pl then run:
>
> perl -MO=Deparse,-p a.pl
>
>
> sub BEGIN {
>    require(v5.14);
> }
> use Test::More;
> use strict 'refs';
> BEGIN {
>    $^H{'feature_unicode'} = q(1);
>    $^H{'feature_say'} = q(1);
>    $^H{'feature_state'} = q(1);
>    $^H{'feature_switch'} = q(1);
> }
> (my(@l) = ('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();
> a.pl syntax OK
>
>
> From this the actual line is
>
> (my $has_red_2 = grep(/red/, (@l ? 1 : 0)));
>
> so as I understand, this  is a precedence issue.
>
> regards
>    Gabor
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20120424/5e856fc9/attachment.htm 


More information about the Perl mailing list