On 5/31/11 Tue May 31, 2011 9:32 AM, "[email protected]"
<[email protected]> scribbled:
> Good Morning,
>
> I could use some help figuring out where a warning is coming from.
>
> My shopping cart script has the following sub:
>
> 695 sub get_ud {
> 696 my ($log, $pass) = $main::global->{login} ?
> ($main::global->{form}->{'userlogin'}, $main::global->{form}->{'userpass'}) :
> ();
> 697 $main::global->{form}->{'ud'} =
> "%%$main::global->{uid}%%$log%%$pass%%".time();
> 698 $main::global->{form}->{'ud'} = Encrypt($main::global->{form}->{'ud'},
> $main::global->{config}->{'cookie'});
> 699 }
>
> These warnings are showing up in the log:
>
> Use of uninitialized value $log in concatenation (.) or string at line 697
> (#1)
> Use of uninitialized value $log in concatenation (.) or string at line 697.
> Use of uninitialized value $pass in concatenation (.) or string at line 697
> (#1)
> Use of uninitialized value $pass in concatenation (.) or string at line 697.
>
> Since both $log and $pass are defined in line 696, why is perl complaining
> about them in line 697? I know they're only warnings, but is there a way to
> eliminate them? I understand that I could use 'no warnings' in this sub, but
> I'd really like to understand what's causing them in the first place since I'm
> getting similar warnings throughout this script.
$log and $pass are ASSIGNED in line 696, but they are apparently being
assigned an undefined value (undef in Perl-speak). If the value of
$main::global->{login} is false, $log and $pass will be assigned values from
the empty list (), and will be therefore undefined. Even if
$main::global->{login} is true, the values being assigned to $log and $pass
may be undefined, depending upon what is in
$main::global->{form}->{'userlogin'} and $main::global->{form}->{'ud'};
You can assign a defined value, e.g. '' or 0, to the variables if they are
undefined like this:
$log = defined $log ? $log : '';
or like this (since undef is false in a boolean expression):
$log = $log || '';
or shorter:
$log ||= '';
You could also assign the list ('','') or ('')x2 instead of the list () in
line 696, although that will only cure one of the two possible causes.
>
> Also, what does the "(#1)" signify?
Don't know.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/