#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Question Symbolic Reference with My Variables


    Why My variable declaration is not working with Symbolic References.

    What is the reason or the limitations of My Variable when it comes with Symbolic reference. Also, pls advise me what is wrong in the below code

    my $jim = 'Hello World!';
    my $var_name = 'jim';
    print ${$var_name}; # Not working getting error
    print $$var_name; # Getting error as Use of uninitialized value in print at ..
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Symbolic references only work with global, not lexical vars.

    DON"T use symbolic references, use a hash.

    Why it's stupid to 'use a variable as a variable name' Par1 1
    Part 2
    Part 3
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Why not with lexical vars


    [QUOTE=FishMonger]Symbolic references only work with global, not lexical vars.

    DON"T use symbolic references, use a hash.

    Hi,

    Thanks for your quick reply.

    I like to clear on "Why symbolic reference is not with Lexical Variables" . Pls clarify.

    I have read many pages in google but still not come to a conclusion.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Symbolic references only work with package globals, which reside in the symbol table. Vars declared with the 'my' keyword are lexical and don't reside in the symbol table.

    When using symbolic references, you can't be running under the strict pragma. More specifically, you can't be running under
    Code:
    use strict 'refs';
    Not using the strict pragma is a mistake and will lead to buggy code.

    Why do you want to use symbolic references?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Just to know about the functionality


    Originally Posted by FishMonger
    Symbolic references only work with package globals, which reside in the symbol table. Vars declared with the 'my' keyword are lexical and don't reside in the symbol table.

    When using symbolic references, you can't be running under the strict pragma. More specifically, you can't be running under
    Code:
    use strict 'refs';
    Not using the strict pragma is a mistake and will lead to buggy code.

    Why do you want to use symbolic references?
    Hi FishMonger,

    Thanks for your reply.

    Just want to know about the functionality of sym. reference. Though it is having many disadvantages, still perl programmers using it somewhere usefully. This is my anxiety.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Just because something can be done, doesn't mean that it should be done and lots of programmers do things that they shouldn't. So, don't jump of a cliff just because someone else did.

    I've lost track of the link, but there is an online lecture that Larry Wall gave where he said that putting symbolic references in the language was a mistake, but it's kept in for backwards compatibility.

IMN logo majestic logo threadwatch logo seochat tools logo