[Israel.pm] boolean statment as sub parameter

Yitzchak Scott-Thoennes sthoenna at efn.org
Sun Feb 22 12:02:36 PST 2009

On Sun, February 22, 2009 10:36 am, ik wrote:
> demo($var1, ($id >= 1) and data_exists(...), "18");

To add a little to what Gabor said:

"and" and "&&" differ in precedence.  "and" is designed to be used in flow
control, "&&" to be used in shorter expressions.

So, you might say something like:

supply_default_values() and $x = 123;

which, using &&, would need to be explicitly parenthesized:

supply_default_values() && ( $x = 123 );

(It's actually kind of hard to come up with good "and" examples; "or"
is a lot more useful.)

But even with &&, you have a potential problem:
Subroutine parameters are in list context.  && will force it's
left operand into scalar context, but propagates the context of
the &&-expression onto the right operand, so data_exists() is
in list context.  If $id is in fact >= 1 and data_exists returns
0 elements, or 2 or more, you aren't going to have the parameters
you expect.  People often get into this trouble using CGI::param in
subroutine parameters or array or hash initializers.

More information about the Perl mailing list