On 29 Jun 2001 09:26:55 -0400, Kim Green wrote:
> What's the proper syntax to indicate that a variable is optional? The script
> that I have created works great when I pass in a variable, but the script
> need to execute the SQL even if I don't pass in a variable.
> 
> Thanks,
> Kim
> 
>

you might try

$var = $ARGV[0] || DEFAULT;

where DEFAULT is some reasonable value.  The || operator returns the
first true value; if $ARGV[0] has not been set on the command line then
it will contain undef which evaluates to false in boolean context so the
default gets returned to the assignment operator.

IMPORTANT: this construct will fail if the argument is the number 0.

In cases where you expect 0 to be a valid value it may be better to use
this construct:

$var = defined($ARGV[0]) ? $ARGV[0] : DEFAULT;

The ?: operator (the only trinary operator I am aware of) returns the
second argument if the first argument is true, otherwise it returns the
third argument.  
 
--
Today is Setting Orange, the 34th day of Confusion in the YOLD 3167
Hail Eris!


Reply via email to