Q: "my" variables and "no strict 'refs'"

Discussion in 'Perl Misc' started by Daniel Friedman, Jun 26, 2003.

  1. The bottom of p.594 in the Camel book (3rd edition) says this:

    no strict 'refs';
    $name = "variable";
    $$name = 7 ; # Sets $variable to 7

    ....but I don't obtain that if I declare the variables with "my".
    Here's my code:

    no strict 'refs';
    my ($variable, $name);
    $variable = 1;
    $name = "variable";
    $$name = 7; # should set $variable to 7

    print "\$variable is $variable\n";

    .... and this prints for me:

    $variable = 1

    I've found the code works fine if I don't declare the variables (with
    the "my" line). What am I not understanding here?

    (I'm using Perl 5.6.0 on RH7.2, in case it makes a difference.)

    Thanks very much,
    --daniel
     
    Daniel Friedman, Jun 26, 2003
    #1
    1. Advertising

  2. (Daniel Friedman) writes:

    > The bottom of p.594 in the Camel book (3rd edition) says this:
    >
    > no strict 'refs';
    > $name = "variable";
    > $$name = 7 ; # Sets $variable to 7


    Don't do that.

    See <http://perl.plover.com/varvarname.html> for reasons why.

    > ...but I don't obtain that if I declare the variables with "my".


    Yes, that's correct.

    perldoc -q "How can I use a variable as a variable name?"

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
     
    Eric Schwartz, Jun 26, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    959
  2. Replies:
    9
    Views:
    997
  3. Noel Sant

    Array of hashes and strict refs - help!

    Noel Sant, Jan 30, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    283
    Ben Morrow
    Jan 30, 2004
  4. Billy Patton

    using no strict "refs"

    Billy Patton, Jan 10, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    246
    Mumia W. (on aioe)
    Jan 11, 2007
  5. bpatton

    no strict 'refs' need help

    bpatton, Jul 12, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    143
    Brian McCauley
    Jul 12, 2007
Loading...

Share This Page