mhearse has asked for the wisdom of the Perl Monks concerning the following question:

An amateur question regarding the evaluation of scalars. I have a shared routine which gets CGI params like so:
sub get_my_params { my @array; $array[0] = defined (param('ticket')) ? param('ticket') : ""; $array[1] = defined (param('title')) ? param('title') : ""; $array[2] = defined (param('client')) ? param('client') : ""; $array[3] = defined (param('next_step')) ? param('next_step') : " +"; $array[4] = defined (param('o_date')) ? param('o_date') : ""; $array[5] = defined (param('o_time')) ? param('o_time') : ""; return @array; }
Then in the callin script:
require "tickets.pm"; my ($ticket, $title, $client, $next_step, $o_date, $o_time) = get_my_p +arams();
During the script I want to evaluate some of the variables. They evaluate for truth correctly:
if ($title) {}
But if I try
if (defined ($title)) {}
I always get a match, even if no parameter was passed. An empty "" isn't defined, is it? When declaring empty variables, should I use undef instead of ""?

Replies are listed 'Best First'.
Re: Scalar evaluation
by Joost (Canon) on Mar 06, 2005 at 21:54 UTC
    "" is defined, the only value that isn't defined is undef.

    So, if you remove the whole defined() ? : checking in get_my_params(), and just assing the param() values to the array elements your code should work.

    update: unless you actually get an empty string as input (as you would, when you have html textfields), in that case it's probably better to check for $input eq ''

Re: Scalar evaluation
by phroggy (Monk) on Mar 06, 2005 at 23:47 UTC

    Yes, an empty string is defined; if you want to see whether a defined string is empty or not, use ($title ne '').

    If it's OK for these values to be undef (if you'll be checking defined($title) before printing them or whatever), you may want to consider:

    sub get_my_params { return(param('ticket'), param('title'), param('client'), param('next_step'), param('o_date'), param('o_time') ); }

    But what you have is good, if you never want them to be undef.

    for $a(-2,12){for $b(0..7){$c=0;$_?hex substr( ef7fa1866706caeff02289402844,2*$_+$a,2)&2**(7-$b):0 and $c+=2**(7-$_)for(0..7);print chr $c;}}
      But IIRC param could return multiple values in list context (if we are talking about CGI::param). Better:
      sub get_my_params { map scalar(param($_)), qw/ticket title client next_step o_date o_ti +me/; }
Re: Scalar evaluation
by dragonchild (Archbishop) on Mar 07, 2005 at 13:31 UTC
    Nothing to do with your original question, but you can do the following rewrite
    # Exactly equivalent drop-in replacement sub get_my_params { map { defined param( $_ ) ? param( $_ ) : '' } qw( ticket title client next_step o_date o_time ); }
    However, better would be to rewrite it as
    # Returns a hash, not an array sub get_my_params { map { $_ => defined param( $_ ) ? param( $_ ) : '' } qw( ticket title client next_step o_date o_time ); } # Use this one as so: my %params = get_my_params(); if ( $params{ title } ne '' ) { # Do stuff here. }
    The use of a hash is better because you can add new parameters to get_my_params() and the only thing that changes is the new code you add. You don't need to keep track of a bazillion parameter names.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.