use strict with an undeclared variable

K

ko

I was looking for an efficient way to find the largest key (assuming all
numeric keys) in a *large* hash and found an excellent solution here
(sorry for the long link):

http://groups.google.com/[email protected]#link5

the relevant code:

foreach ($max = each %hash;
defined ($a = each %hash);
$max = $a if $a > $max) {}

Took me a while to figure it out - most of the time you see 'foreach'
being shortened to 'for' instead of the other way around.

Anyway, there are two things I can't figure out.

1. If you're including the strict and warnings pragmas, why is it that
you can get away with not declaring $a? I thought defined() might have
something to do with it, but the docs only say that when used on a hash
element it tells you whether the value is defined.

2. Is defined() necessary in the condition? The assignment of each key
to $a returns true. I think there was a reference in the thread to why
defined is used, but I didn't understand the explanation, so I'm not sure.

For anyone who might suggest sort() or iterating with keys() to
accomplish the task, please follow the link above for explanations to
why this method is more efficient.

Thanks in advance - keith
 
G

Greg Bacon

: [...]
:
: 1. If you're including the strict and warnings pragmas, why is it that
: you can get away with not declaring $a? [...]

The strict pragma's manpage says

Because of their special use by sort(), the variables
$a and $b are exempted from this check.

: 2. Is defined() necessary in the condition? The assignment of each key
: to $a returns true. [...]

What if the hash contains a 0 key?

: [...]

Hope this helps,
Greg
 
K

ko

Greg said:
: [...]
:
: 1. If you're including the strict and warnings pragmas, why is it that
: you can get away with not declaring $a? [...]

The strict pragma's manpage says

Because of their special use by sort(), the variables
$a and $b are exempted from this check.

Sorry, I must be missing something obvious. I don't see where sort() is
being called. I thought that a strict comparison is being done and the
whole point was to avoid using sort.
: 2. Is defined() necessary in the condition? The assignment of each key
: to $a returns true. [...]

What if the hash contains a 0 key?

: [...]

That I understand - thanks!
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Greg said:
: [...]
:
: 1. If you're including the strict and warnings pragmas, why is it
: that you can get away with not declaring $a? [...]

The strict pragma's manpage says

Because of their special use by sort(), the variables
$a and $b are exempted from this check.

Sorry, I must be missing something obvious. I don't see where sort()
is being called. I thought that a strict comparison is being done and
the whole point was to avoid using sort.

Because there exists such a thing as 'sort' (and because of how 'sort'
works in Perl), the variables $a and $b are exempted from this check.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP3biK2PeouIeTNHoEQLCQwCdHqBTYZZdZpm7zy8Tks+Hf+D+DNcAoIS7
xD2FjHrq1mSEBazqj12jkBv2
=fG0i
-----END PGP SIGNATURE-----
 
K

ko

Eric J. Roode said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Greg said:
: [...]
:
: 1. If you're including the strict and warnings pragmas, why is it
: that you can get away with not declaring $a? [...]

The strict pragma's manpage says

Because of their special use by sort(), the variables
$a and $b are exempted from this check.

Sorry, I must be missing something obvious. I don't see where sort()
is being called. I thought that a strict comparison is being done and
the whole point was to avoid using sort.

Because there exists such a thing as 'sort' (and because of how 'sort'
works in Perl), the variables $a and $b are exempted from this check.

- --
Eric

Sometimes I need literal explanations :( Is it correct to say that $a
and $b are only exempt inside a condition and loop? For example, you
must declare $a or $b just like any other variable when used in the
main body of a program/sub:

[SNIPPET]
#!/usr/bin/perl -w
use strict;
$a = 1;

[RESULT]
Name "main::a" used only once: possible typo at f:\perl\a.pl line 3.

Thanks for bearing with me...
 
S

Steve Grazzini

ko said:
Sometimes I need literal explanations :( Is it correct to say that $a
and $b are only exempt inside a condition and loop?

No. They're always exempt. (Try it and see.)
For example, you
must declare $a or $b just like any other variable when used in the
main body of a program/sub:

[SNIPPET]
#!/usr/bin/perl -w
use strict;
$a = 1;

[RESULT]
Name "main::a" used only once: possible typo at f:\perl\a.pl line 3.

That's different. The "used only once" warning has nothing to do
with strict. If $a weren't exempt from strict-checking, you'd get
a compilation error. (Try it with $c.)
 
K

ko

Steve Grazzini said:
ko said:
Sometimes I need literal explanations :( Is it correct to say that $a
and $b are only exempt inside a condition and loop?

No. They're always exempt. (Try it and see.)
For example, you
must declare $a or $b just like any other variable when used in the
main body of a program/sub:

[SNIPPET]
#!/usr/bin/perl -w
use strict;
$a = 1;

[RESULT]
Name "main::a" used only once: possible typo at f:\perl\a.pl line 3.

That's different. The "used only once" warning has nothing to do
with strict. If $a weren't exempt from strict-checking, you'd get
a compilation error. (Try it with $c.)

And he *finally* sees the light! For some reason I had it stuck in my
head that $a and $b were only special within a sort routine, and have
always associated special variables with uppercased alphabetic or
punctuation characters.

Thanks for the straightforward explanation.
 
E

Eric Schwartz

And he *finally* sees the light! For some reason I had it stuck in my
head that $a and $b were only special within a sort routine, and have
always associated special variables with uppercased alphabetic or
punctuation characters.

Personally, I recommend avoiding them except in sort routines anyway.
I find it confusing to see $a and $b in otherwise strict-safe code,
and usually spend a minute or two looking for their definition until I
remember that they don't need one.

-=Eric
 
K

ko

Eric Schwartz said:
Personally, I recommend avoiding them except in sort routines anyway.
I find it confusing to see $a and $b in otherwise strict-safe code,
and usually spend a minute or two looking for their definition until I
remember that they don't need one.

-=Eric

Actually I make it a point to not use variables that don't somehow
relate (in my mind at least) to what I'm trying to do - more
specifically, I only use $a and $b in sort routines.

My lame excuse for why it took me so long to figure this one out :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top