Use of uninitialized value warnings

Use of uninitialized value warnings

am 31.05.2011 18:32:53 von sono-io

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) =3D $main::global->{login} ? =
($main::global->{form}->{'userlogin'}, =
$main::global->{form}->{'userpass'}) : ();
697 $main::global->{form}->{'ud'} =3D =
"%%$main::global->{uid}%%$log%%$pass%%".time();
698 $main::global->{form}->{'ud'} =3D =
Encrypt($main::global->{form}->{'ud'}, =
$main::global->{config}->{'cookie'});
699 }

These warnings are showing up in the log:

=95 Use of uninitialized value $log in concatenation (.) or string at =
line 697 (#1)
=95 Use of uninitialized value $log in concatenation (.) or string at =
line 697.
=95 Use of uninitialized value $pass in concatenation (.) or string at =
line 697 (#1)
=95 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.

Also, what does the "(#1)" signify?

If I've left anything out, please let me know.

Thank you,
Marc

Perl 5.12.3
Mac OS 10.6.7


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Use of uninitialized value warnings

am 31.05.2011 18:54:28 von Jim Gibson

On 5/31/11 Tue May 31, 2011 9:32 AM, "sono-io@fannullone.us"
scribbled:

> Good Morning,
>=20
> I could use some help figuring out where a warning is coming from.
>=20
> My shopping cart script has the following sub:
>=20
> 695 sub get_ud {
> 696 my ($log, $pass) =3D $main::global->{login} ?
> ($main::global->{form}->{'userlogin'}, $main::global->{form}->{'userpass'=
}) :
> ();
> 697 $main::global->{form}->{'ud'} =3D
> "%%$main::global->{uid}%%$log%%$pass%%".time();
> 698 $main::global->{form}->{'ud'} =3D Encrypt($main::global->{form}->{'ud'}=
,
> $main::global->{config}->{'cookie'});
> 699 }
>=20
> These warnings are showing up in the log:
>=20
> =80 Use of uninitialized value $log in concatenation (.) or string at line =
697
> (#1)
> =80 Use of uninitialized value $log in concatenation (.) or string at line =
697.
> =80 Use of uninitialized value $pass in concatenation (.) or string at line=
697
> (#1)
> =80 Use of uninitialized value $pass in concatenation (.) or string at line=
697.
>=20
> Since both $log and $pass are defined in line 696, why is perl complainin=
g
> 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 sinc=
e 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 fro=
m
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 =3D defined $log ? $log : '';

or like this (since undef is false in a boolean expression):

$log =3D $log || '';

or shorter:

$log ||=3D '';


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.

>=20
> Also, what does the "(#1)" signify?

Don't know.



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Use of uninitialized value warnings

am 31.05.2011 20:00:22 von sono-io

On May 31, 2011, at 9:54 AM, Jim Gibson wrote:

> $log and $pass are ASSIGNED in line 696, but they are apparently being
> assigned an undefined value (undef in Perl-speak).

Thanks a million, Jim. You were absolutely correct. I didn't =
stop to think about the variable result being undefined. =3D:\ After I =
defined them as you mentioned, the warnings went away.

Thanks again,
Marc=

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Use of uninitialized value warnings

am 01.06.2011 12:00:52 von am0c am0c

If you use

$log = $log || '';

instead of

$log = defined $log ? $log : '';

It might yield a problem because when $log is "0" it is defined but
false in boolean context.
The correct shorter version is:

$log = $log // '';

And in the line 696:

> 696 my ($log, $pass) = $main::global->{login} ?
> ($main::global->{form}->{'userlogin'}, $main::global->{form}->{'userpass'}) :
> ();

When $main::global->{login} is false, $log and $pass will not be defined anyway.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Use of uninitialized value warnings

am 01.06.2011 18:00:36 von Uri Guttman

>>>>> "aa" == am0c am0c writes:

aa> If you use
aa> $log = $log || '';

that should be $log ||= '' ;

aa> instead of

aa> $log = defined $log ? $log : '';

aa> It might yield a problem because when $log is "0" it is defined but
aa> false in boolean context.
aa> The correct shorter version is:

aa> $log = $log // '';

same here:

$log //= '';

also the use of // for defined or is relatively recent in perl. be
careful when using it as it may not work in the version you have
installed or in production.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Use of uninitialized value warnings

am 06.06.2011 10:58:45 von Chris Nehren

On Wed, Jun 01, 2011 at 12:00:36 -0400 , Uri Guttman wrote:
> also the use of // for defined or is relatively recent in perl. be
> careful when using it as it may not work in the version you have
> installed or in production.

Relatively recent meaning 3.5 years old, and released in a version of
perl that has been officially EOLd by the perl5 porters (giving way to
5.12 and 5.14). It's only an issue if you're on an old, not-updated
RedHat, an ancient Debian (old-old stable, maybe older still), or an old
Solaris. Maybe some other rather older systems.

Seriously, in all the client and work machines I've used, I've not had
to worry about using 5.10 features. If I can use them without qualms in
code targetting Debian and RedHat, most folks won't have to worry.

--
Chris Nehren | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Use of uninitialized value warnings

am 06.06.2011 11:04:42 von Uri Guttman

>>>>> "CN" == Chris Nehren writes:

CN> On Wed, Jun 01, 2011 at 12:00:36 -0400 , Uri Guttman wrote:
>> also the use of // for defined or is relatively recent in perl. be
>> careful when using it as it may not work in the version you have
>> installed or in production.

CN> Relatively recent meaning 3.5 years old, and released in a version of
CN> perl that has been officially EOLd by the perl5 porters (giving way to
CN> 5.12 and 5.14). It's only an issue if you're on an old, not-updated
CN> RedHat, an ancient Debian (old-old stable, maybe older still), or an old
CN> Solaris. Maybe some other rather older systems.

and i was doing a project this past year (aug 2010 - mar 2011) where i
used // in my code since it was fine on my machine. i had to remove it
since the production server was running 5.8 and the boss wouldn't
upgrade it. not much i could do about that.

// is cool but is a minor improvement you can work around very
easily. and || is usually fine for that if you design your var not to
have 0 or '' for a possible value. so the need for // is minimal but it
is still cool to have.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/