Perl FAQ error in "variable as a variable name"

E

Eric Pement

In trying to learn about references, I believe I've found an error in
the Perl FAQ, section 7 ("General Perl Language Issues"), under the
question, "How can I use a variable as a variable name?" The problem
exists in the documentation for Perl 5.6 and Perl 5.8. I tried
emailing the FAQ keepers a while ago and got no response. Maybe
somebody can confirm or correct me here. (smile)

The FAQ currently reads in part:

By using symbolic references, you are just using the package's
symbol-table hash (like %main::) instead of a user-defined hash.
The solution is to use your own hash or a real reference instead.

$fred = 23;
$varname = "fred";
$USER_VARS{$varname}++; # not $$varname++

This solution does not work and does not provide "24" as one would
expect. You can prove it by just running the script:

#!/usr/bin/perl
use strict;
my ($fred, $varname, %USER_VARS);
$fred = 23;
$varname = "fred";
$USER_VARS{$varname}++;
print "Value is: $USER_VARS{$varname}\n";

The output is "Value is: 1". So how should this be corrected? Here's
the replacement I suggest for the FAQ. It returns "24", as expected:

$USER_VARS{fred} = 23;
$varname = "fred";
$USER_VARS{$varname}++; # not $$varname++

Is this indeed the "proper" or intended solution?

Finally, the FAQ says another solution is to use a real reference
instead. Am I correct in thinking that such a solution would look like
this, or would this not meet the needs of the original "request" posed
in the Perl FAQ?

$fred = 23;
$varname = \$fred;
$$varname++;

Thanks in advance for courteous treatment here.

Eric Pement
 
J

James E Keenan

Eric Pement said:
In trying to learn about references, I believe I've found an error in
the Perl FAQ, section 7 ("General Perl Language Issues"), under the
question, "How can I use a variable as a variable name?" The problem
exists in the documentation for Perl 5.6 and Perl 5.8. I tried
emailing the FAQ keepers a while ago and got no response. Maybe
somebody can confirm or correct me here. (smile)

The FAQ currently reads in part:

By using symbolic references, you are just using the package's
symbol-table hash (like %main::) instead of a user-defined hash.
The solution is to use your own hash or a real reference instead.

$fred = 23;
$varname = "fred";
$USER_VARS{$varname}++; # not $$varname++

This solution does not work and does not provide "24" as one would
expect. You can prove it by just running the script:
No, one would *not* expect '24' as the result. You're not doing anything
with $fred once you've assigned '23' to it. Until you make some assignment,
$USERS_VARS{'fred'} == 0, which when ++ed makes 1.
 
J

Jay Tilton

(e-mail address removed) (Eric Pement) wrote:

: Finally, the FAQ says another solution is to use a real reference
: instead. Am I correct in thinking that such a solution would look like
: this,

: $fred = 23;
: $varname = \$fred;
: $$varname++;

Exactly correct.
 
E

Eric Pement

James E Keenan said:

[... snip ...]
No, one would *not* expect '24' as the result. You're not doing anything
with $fred once you've assigned '23' to it.

Which is exactly what my posting demonstrated. I should have worded my
comment more precisely: "This solution does not work and does not provide
'24' as the authors of the Perl FAQ thought it would."

It was an accident on their part, and with such a large document it
is almost inevitable that errors will creep in here and there. Hopefully
this one will be corrected in the next edition.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top