symbolic reference

Discussion in 'Perl' started by slurper, Jan 29, 2004.

  1. slurper

    slurper Guest

    i don't understand following snippet from "programming perl".

    $name = "bam";
    $$name = 1; # Sets $bam
    ${$name} = 2; # Sets $bam
    ${$name x 2} = 3; # Sets $bambam
    $name->[0] = 4; # Sets $bam[0]
    i don't understand above. $name is a string and it gets dereferenced.
    impossible no?
    can anyone shed some light on this
    tx very much

    @$name = (); # Clears @bam
    &$name(); # Calls &bam() (as in prior versions of Perl)
    $pkg = "THAT"; # (Don't use "package" or "pack"!)
    ${"${$pkg}::$name"} = 5; # Sets $THAT::bam without eval
     
    slurper, Jan 29, 2004
    #1
    1. Advertising

  2. slurper

    Guest

    slurper <> wrote in message news:<401977bd$0$772$>...
    > i don't understand following snippet from "programming perl".
    >
    > $name = "bam";
    > $$name = 1; # Sets $bam
    > ${$name} = 2; # Sets $bam
    > ${$name x 2} = 3; # Sets $bambam
    > $name->[0] = 4; # Sets $bam[0]
    > i don't understand above.


    How so?

    > $name is a string and it gets dereferenced.


    Yep that's correct. That is what symbolic reference means. It means
    using a string as a reference.

    > impossible no?


    No.

    When Perl encounters a string when it expects a reference it will, by
    default, take that string and perform lookup in the package symbol
    table(s) to get the reference. Note: it only looks in the package
    symbol tables (aka "the stashes"), not the lexical symbol table (aka
    "the pad").

    Although this feature is, for historical reasons, switched on by
    default it is rarely needed can be very confusing if you are not
    expecting it. It can (and should) be switched off when not needed.
    For this reason it is best practice to put "use strict" at the top of
    all your scripts and modules. Doing so switches the symbolic
    reference feature off (except in a handfull of special cases). It
    also switches off two other unrelated but equally potentially
    confusing legacy features.

    If you have a good reason to use symbolic references you can enable
    them just for the current BLOCK with "no strict 'refs'".

    > can anyone shed some light on this


    perldoc perlref

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.
     
    , Jan 30, 2004
    #2
    1. Advertising

  3. slurper

    Anthony Guest

    $name = "bam";
    $$name = 1; # this also means ${bam} = 1. This is because of
    interpolation. This could be used in interesting applications, but
    will make debugging seriously complicated.




    slurper <> wrote in message news:<401977bd$0$772$>...
    > i don't understand following snippet from "programming perl".
    >
    > $name = "bam";
    > $$name = 1; # Sets $bam
    > ${$name} = 2; # Sets $bam
    > ${$name x 2} = 3; # Sets $bambam
    > $name->[0] = 4; # Sets $bam[0]
    > i don't understand above. $name is a string and it gets dereferenced.
    > impossible no?
    > can anyone shed some light on this
    > tx very much
    >
    > @$name = (); # Clears @bam
    > &$name(); # Calls &bam() (as in prior versions of Perl)
    > $pkg = "THAT"; # (Don't use "package" or "pack"!)
    > ${"${$pkg}::$name"} = 5; # Sets $THAT::bam without eval
     
    Anthony, Jan 30, 2004
    #3
  4. slurper

    Joe Smith Guest

    slurper wrote:
    > i don't understand following snippet from "programming perl".


    First thing to understand is that $variable_name and
    ${variable_name} are the same thing. Second, ${"variable_name"}
    and $_="variable_name";${$_} also reference $variable_name.

    > $name = "bam";
    > $$name = 1; # Sets $bam
    > ${$name} = 2; # Sets $bam
    > ${$name x 2} = 3; # Sets $bambam
    > $name->[0] = 4; # Sets $bam[0]
    > i don't understand above. $name is a string and it gets dereferenced.


    $name is a string. It's value is being used as the name of another
    variable.
     
    Joe Smith, Jan 30, 2004
    #4
    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. Paul Urbanus
    Replies:
    4
    Views:
    3,372
    Tim Hubberstey
    Jan 12, 2005
  2. Jim
    Replies:
    4
    Views:
    7,012
  3. Matthew Macchia
    Replies:
    3
    Views:
    1,072
    Roedy Green
    Apr 13, 2004
  4. Juha Laiho
    Replies:
    0
    Views:
    677
    Juha Laiho
    Jul 20, 2004
  5. Jesper Sahner

    "Symbolic computation"

    Jesper Sahner, Oct 23, 2004, in forum: Java
    Replies:
    3
    Views:
    437
Loading...

Share This Page