symbolic reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slurper

    symbolic reference

    i don't understand following snippet from "programmin g 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}::$na me"} = 5; # Sets $THAT::bam without eval
  • nobull@mail.com

    #2
    Re: symbolic reference

    slurper <slurper1234@sk ynet.be> wrote in message news:<401977bd$ 0$772$ba620e4c@ news.skynet.be> ...[color=blue]
    > i don't understand following snippet from "programmin g 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.[/color]

    How so?
    [color=blue]
    > $name is a string and it gets dereferenced.[/color]

    Yep that's correct. That is what symbolic reference means. It means
    using a string as a reference.
    [color=blue]
    > impossible no?[/color]

    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'".
    [color=blue]
    > can anyone shed some light on this[/color]

    perldoc perlref

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.

    Comment

    • Anthony

      #3
      Re: symbolic reference

      $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 <slurper1234@sk ynet.be> wrote in message news:<401977bd$ 0$772$ba620e4c@ news.skynet.be> ...[color=blue]
      > i don't understand following snippet from "programmin g 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}::$na me"} = 5; # Sets $THAT::bam without eval[/color]

      Comment

      • Joe Smith

        #4
        Re: symbolic reference

        slurper wrote:[color=blue]
        > i don't understand following snippet from "programmin g perl".[/color]

        First thing to understand is that $variable_name and
        ${variable_name } are the same thing. Second, ${"variable_nam e"}
        and $_="variable_na me";${$_} also reference $variable_name.
        [color=blue]
        > $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.[/color]

        $name is a string. It's value is being used as the name of another
        variable.

        Comment

        Working...