[Israel.pm] boolean statment as sub parameter

ik idokan at gmail.com
Sun Feb 22 11:21:17 PST 2009


Thank you Gabor it helped me a lot.

Ido



On Sun, Feb 22, 2009 at 9:14 PM, Gabor Szabo <szabgab at gmail.com> wrote:
> On Sun, Feb 22, 2009 at 8:36 PM, ik <idokan at gmail.com> wrote:
>> Hello,
>>
>> I've created a sub like this:
>>
>> sub demo {
>>  my $paramA = shift;
>>  my $paramB = shift;
>>  my $paramC = shift;
>>  ...
>> }
>>
>> when I used the sub like so:
>>
>> demo($var1, ($id >= 1) and data_exists(...), "18");
>>
>> the sub does not have any parameters that exists at @_ (if I print it
>> using print Dumper(@_) prior on using shift, however when I use it
>> like so:
>>
>> my $bool =  ($id >= 1) and data_exists(...);
>> demo($var1, $bool, "18");
>>
>> the parameters passes properly.
>>
>> Am I missing here something or doing it wrong or what is going on ?
>> I'm using perl v5.10.0 under Debian Linux (x86_64).
>
> IMHO both.
> 1) when using Dumper of Data::Dumper you better pass a reference to it
>    so write:
>    print Dumper \@_;
>
> 2) I am quite sure you will see an array with one element there
>
> 3) try running your code with:
>   perl -MO=Deparse script.pl
>
> for example I created the following script:
>
>   demo($var1, ($id >= 1) and data_exists(2), "18");
>
> (I had to replace the ... with something that will compile)
>
> then I ran it through B::Deparse using the above command and I got:
>
>   demo(($var1, $id >= 1) && (data_exists(2), '18'));
>
> That's how perl understood your code.
>
> The point is that  comma  (,) has higher precedence than 'and'
> thus perl puts the parentheses in other places than what you meant.
> If you replaced the 'and' with '&&' that would probably solve your problem.
>
>   demo($var1, ($id >= 1) && data_exists(2), "18");
>
> Better yet put the condition in parentheses:
>
>   demo($var1, ($id >= 1 && data_exists(2)), "18");
>
>
> See perldoc perlop for the precedence table
> See perldoc B::Deparse for, well B::Deparse
>
> Gabor
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



More information about the Perl mailing list