Eric is correct, the FAQ is wrong.

The simplest patch is probably:

--- perlfaq7.pod        31 Jan 2003 17:38:14 -0000      1.14
+++ perlfaq7.pod        20 Jul 2003 06:09:30 -0000
@@ -809,5 +809,5 @@
 use your own hash or a real reference instead.
 
-    $fred    = 23;
+    $USER_VARS{"fred"} = 23;
     $varname = "fred";
     $USER_VARS{$varname}++;  # not $$varname++





=== Forwarded Message:

Date: Fri, 11 Jul 2003 09:40:56 -0500
From: Eric Pement <[EMAIL PROTECTED]>
Subject: Error in Sect. 7 on using a variable as a variable name
To: [EMAIL PROTECTED]

To whom it may concern:

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 this message to "[EMAIL PROTECTED]" and got
no response. Now that I've got a newer email address, perhaps I can
get a reply.

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 the FAQ
authors seem to have intended. This can be demonstrated like so:

    #!/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
a replacement I suggest for the FAQ. It returns "24", as expected:

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

Finally, the FAQ says another solution is to use a real reference
instead. Perhaps you could tell new perl users how this is done:

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

Thanks in advance for your time.


-- 
Eric Pement - [EMAIL PROTECTED]
Education Technical Services, MBI

=== End of Forwarded Message

Reply via email to