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
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