Using a string as a variable name.

Discussion in 'Perl Misc' started by Jeff, Oct 5, 2004.

  1. Jeff

    Jeff Guest

    Hi,

    I want to do things like this:

    my $v1 = "varname";
    my $varname = "it worked!";
    print ${$v1};

    And have the final statement know that it should print the content of $varname.

    Is it possible?

    Thanks,


    Jeff
    Jeff, Oct 5, 2004
    #1
    1. Advertising

  2. Jeff wrote:
    > I want to do things like this:


    No, you don't. Please read the FAQ (perldoc -q "variable name") and
    gazillions of previous articles why this is a _very_ bad idea.

    > my $v1 = "varname";
    > my $varname = "it worked!";
    > print ${$v1};
    >
    > And have the final statement know that it should print the content of
    > $varname.
    >
    > Is it possible?


    Technically yes. But there is no good reason to use symbolic references
    unless you do want to modify the system symbol table.

    Just use a hash instead. Much easier to use, to maintain, and without all
    the problems associated with symbolic references.

    jue
    Jürgen Exner, Oct 5, 2004
    #2
    1. Advertising

  3. Jeff

    Paul Lalli Guest

    "Jeff" <> wrote in message
    news:...
    > Hi,
    >
    > I want to do things like this:
    >
    > my $v1 = "varname";
    > my $varname = "it worked!";
    > print ${$v1};
    >
    > And have the final statement know that it should print the content of

    $varname.
    >
    > Is it possible?


    The standard response is "You don't want to do that. Read perldoc -q
    "variable name" to understand why.

    However, to answer the question you actually asked, yes, you are
    technically able to do that - provided that
    1) the variables are package variables, not lexicals (in your example
    program, you'd have to change my to our, or disable strict vars (which
    you also don't want to do))
    2) strict refs has also been disabled (which yet again, is something
    you don't want to do.)

    In other words, "DON'T DO THIS". Use a hash. Read perldoc -q "variable
    name"

    Paul Lalli
    Paul Lalli, Oct 5, 2004
    #3
  4. Jeff

    Tore Aursand Guest

    On Tue, 05 Oct 2004 09:34:38 -0700, Jeff wrote:
    > I want to do things like this:


    No. You _think_ you want this, but actually you don't. Please refer to
    'perldoc -q "variable name"' for more information on why you should avoid
    this.

    > my $v1 = "varname";
    > my $varname = "it worked!";
    > print ${$v1};


    Better off using a hash;

    my %variables = ('v1' => 'it',
    'v2' => 'worked');

    print "$variables{v1} $variables{v2}\n";


    --
    Tore Aursand <>
    "The road to hell is full of good intentions." (Bruce Dickinson)
    Tore Aursand, Oct 5, 2004
    #4
  5. Jeff wrote:

    > Hi,
    >
    > I want to do things like this:
    >
    > my $v1 = "varname";
    > my $varname = "it worked!";
    > print ${$v1};
    >
    > And have the final statement know that it should print the content of
    > $varname.
    >
    > Is it possible?
    >

    You want to use a hash.

    my %hashname;
    $hashname{'keyname'}="it worked!";
    my $v1="keyname";
    print $hashname{$v1};

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Oct 5, 2004
    #5
  6. Abigail wrote:

    > Jeff () wrote on MMMMLIII September MCMXCIII in
    > <URL:news:>:
    > `' Hi,
    > `'
    > `' I want to do things like this:
    > `'
    > `' my $v1 = "varname";
    > `' my $varname = "it worked!";
    > `' print ${$v1};
    > `'
    > `' And have the final statement know that it should print the content of
    > `' $varname.
    >
    > Oh, I got to ask: why do you want this? Would you want to do such a
    > thing in C or in Java as well?
    >

    No, but a lot of people come to Perl from shell scripting, and doing
    this sort of thing in shell scripts is very common when what you
    really want is a Perl-type hash. Since you generally don't have
    hashes in shell scripting, you use this kind of hack instead, and
    some people try to continue doing it in Perl even though there's
    something 254.32 times better.

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Oct 5, 2004
    #6
  7. Chris Mattern <> wrote:

    > Abigail wrote:
    >
    >> Jeff () wrote on MMMMLIII September MCMXCIII
    >> in <URL:news:>:
    >> `' Hi,
    >> `'
    >> `' I want to do things like this:
    >> `'
    >> `' my $v1 = "varname";
    >> `' my $varname = "it worked!";
    >> `' print ${$v1};
    >> `'
    >> `' And have the final statement know that it should print the
    >> content of `' $varname.
    >>
    >> Oh, I got to ask: why do you want this? Would you want to do such
    >> a thing in C or in Java as well?
    >>

    > No, but a lot of people come to Perl from shell scripting, and
    > doing this sort of thing in shell scripts is very common when what
    > you really want is a Perl-type hash. Since you generally don't
    > have hashes in shell scripting, you use this kind of hack instead,
    > and some people try to continue doing it in Perl even though
    > there's something 254.32 times better.


    I used to do the same sort of thing when writing SAS macros. (For
    those unfamiliar with it, SAS is a statistical package, or at least
    started as one.) SAS macro language has string variables only, so
    even arrays have to be simulated using symbolic references.

    When I started programming in Perl I applied some of the same
    techniques I had used to get around the shortcomings of the SAS macro
    language. When it was pointed out to me that symbolic references were
    a bad idea, and more importantly, WHY they were a bad idea, I never
    used them again. (at least in Perl)

    But when I use SAS, I have no choice: it's symbolic references or
    nothing.
    David K. Wall, Oct 5, 2004
    #7
  8. [completely off-topic] Re: Using a string as a variable name.

    "David K. Wall" <> wrote in
    news:Xns9579B01A9304Ddkwwashere@216.168.3.30:

    > I used to do the same sort of thing when writing SAS macros. (For
    > those unfamiliar with it, SAS is a statistical package, or at least
    > started as one.) SAS macro language has string variables only, so
    > even arrays have to be simulated using symbolic references.

    ....

    > But when I use SAS, I have no choice: it's symbolic references or
    > nothing.


    I will not touch SAS macros with a ten-foot pole. It is impossible to
    decipher what exactly is going on in each step and it is even more
    impossible to figure out what other people are doing.

    Some time ago, I used to use gslgen (http://www.imatix.com/html/gslgen/)
    which would let me come up with a meta-specification in XML for what I
    wanted to do and then translate into SAS or Stata code. That is how much I
    hate SAS macro programming.

    To bring this post somewhat near topicality, Perl makes it even easier for
    me to do this. (And, no, no one has to decipher my XML stuff. I just end up
    with clean SAS files that might appear a little repetitive but are
    straightforward to understand even by novices).

    Sorry, couldn't resist.

    Sinan.
    A. Sinan Unur, Oct 5, 2004
    #8
  9. (Jeff) wrote in
    news::

    > Hi,
    >
    > I want to do things like this:
    >
    > my $v1 = "varname";
    > my $varname = "it worked!";
    > print ${$v1};
    >
    > And have the final statement know that it should print the content of
    > $varname.
    >
    > Is it possible?


    Strange that you could figure out how to print the indirect value out, but
    you couldn't figure out how to assign indirectly. :)

    ${$v1} = "it worked!";

    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
    Eric J. Roode, Oct 9, 2004
    #9
  10. Jeff

    Uri Guttman Guest

    >>>>> "EJR" == Eric J Roode <> writes:

    >> Is it possible?


    EJR> Strange that you could figure out how to print the indirect value
    EJR> out, but you couldn't figure out how to assign indirectly. :)

    EJR> ${$v1} = "it worked!";


    strange that you should know better than to give a symref answer without
    all the usual caveats on how dangerous and wrong it is.

    $hash{$v1} = 'better than just working!' ;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Oct 9, 2004
    #10
  11. Jeff

    Ben Morrow Guest

    Quoth Dan Jones <>:
    > Uri Guttman wrote:
    > >
    > > strange that you should know better than to give a symref answer without
    > > all the usual caveats on how dangerous and wrong it is.
    > >
    > > $hash{$v1} = 'better than just working!' ;

    >
    > Care to expound? What is dangerous about the first example? What's with
    > the "hash" in the second? As I understand it, there was no hash involved -
    > it was the name of a scalar variable stored as a string in another scalar.
    > Is hash a keyword or simply a variable name? If this is a FAQ or a perldoc
    > question, a pointer to the right location would be appreciated. I don't
    > find anything looking there and a Google for $hash doesn't show up anything
    > that seems relevant.


    Google for the several-threads-a-week on this topic (using a variable as
    a variable name, or 'symrefs'), or read perldoc -q variable name. If you
    don't know what a hash is, start with perldoc perldata.

    Ben

    --
    I've seen things you people wouldn't believe: attack ships on fire off
    the shoulder of Orion; I watched C-beams glitter in the dark near the
    Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
    Time to die.
    Ben Morrow, Oct 9, 2004
    #11
  12. Jeff

    Uri Guttman Guest

    >>>>> "DJ" == Dan Jones <> writes:

    DJ> Uri Guttman wrote:

    >> strange that you should know better than to give a symref answer without
    >> all the usual caveats on how dangerous and wrong it is.
    >>
    >> $hash{$v1} = 'better than just working!' ;


    DJ> Care to expound? What is dangerous about the first example?
    DJ> What's with the "hash" in the second? As I understand it, there
    DJ> was no hash involved - it was the name of a scalar variable stored
    DJ> as a string in another scalar. Is hash a keyword or simply a
    DJ> variable name? If this is a FAQ or a perldoc question, a pointer
    DJ> to the right location would be appreciated. I don't find anything
    DJ> looking there and a Google for $hash doesn't show up anything that
    DJ> seems relevant.

    search google for my name and posts about symrefs or symbolic refs. i
    have posted many times as to why they are wrong.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Oct 9, 2004
    #12
  13. Dan Jones wrote:
    > Uri Guttman wrote:
    >> strange that you should know better than to give a symref answer
    >> without all the usual caveats on how dangerous and wrong it is.
    >>
    >> $hash{$v1} = 'better than just working!' ;

    >
    > Care to expound? What is dangerous about the first example?


    You must have missed the gazillions of posts about why symbolic references
    are bad, bad, BAD.

    Why would you want to mess around with the system symbol table (that's what
    you are effectively doing with symrefs) instead of using your own hash? Not
    to mention the implied limitations like local variables, strictures, etc.

    > What's with the "hash" in the second? As I understand it, there was no
    > hash
    > involved -


    Yes, there was. Symbolic references are abusing the internal system symbol
    table hash.

    > it was the name of a scalar variable stored as a string in
    > another scalar. Is hash a keyword or simply a variable name? If this
    > is a FAQ or a perldoc question, a pointer to the right location would
    > be appreciated. I don't find anything looking there and a Google for
    > $hash doesn't show up anything that seems relevant.


    perldoc -q "variable name"
    http://www.google.com/groups?as_epq=symbolic ref&as_ugroup=comp.lang.perl.misc
    http://www.google.com/groups?as_epq=symref&as_ugroup=comp.lang.perl.misc

    jue
    Jürgen Exner, Oct 10, 2004
    #13
  14. Jeff

    Dan Jones Guest

    Uri Guttman wrote:

    >>>>>> "EJR" == Eric J Roode <> writes:

    >
    > >> Is it possible?

    >
    > EJR> Strange that you could figure out how to print the indirect value
    > EJR> out, but you couldn't figure out how to assign indirectly. :)
    >
    > EJR> ${$v1} = "it worked!";
    >
    >
    > strange that you should know better than to give a symref answer without
    > all the usual caveats on how dangerous and wrong it is.
    >
    > $hash{$v1} = 'better than just working!' ;


    Care to expound? What is dangerous about the first example? What's with
    the "hash" in the second? As I understand it, there was no hash involved -
    it was the name of a scalar variable stored as a string in another scalar.
    Is hash a keyword or simply a variable name? If this is a FAQ or a perldoc
    question, a pointer to the right location would be appreciated. I don't
    find anything looking there and a Google for $hash doesn't show up anything
    that seems relevant.
    Dan Jones, Oct 10, 2004
    #14
  15. Uri Guttman <> wrote in
    news::

    >>>>>> "EJR" == Eric J Roode <> writes:

    >
    > >> Is it possible?

    >
    > EJR> Strange that you could figure out how to print the indirect value
    > EJR> out, but you couldn't figure out how to assign indirectly. :)
    >
    > EJR> ${$v1} = "it worked!";
    >
    >
    > strange that you should know better than to give a symref answer without
    > all the usual caveats on how dangerous and wrong it is.
    >
    > $hash{$v1} = 'better than just working!' ;


    Aw, Uri, you're not letting me have any fun! ;-)

    Actually, I didn't feel it necessary, since others had already given good
    reasons not to do so (and to read the FAQ).

    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
    Eric J. Roode, Oct 12, 2004
    #15
  16. Dan Jones <> wrote in
    news::

    > Uri Guttman wrote:
    >
    >>>>>>> "EJR" == Eric J Roode <> writes:

    >>
    >> >> Is it possible?

    >>
    >> EJR> Strange that you could figure out how to print the indirect
    >> value EJR> out, but you couldn't figure out how to assign
    >> indirectly. :)
    >>
    >> EJR> ${$v1} = "it worked!";
    >>
    >>
    >> strange that you should know better than to give a symref answer
    >> without all the usual caveats on how dangerous and wrong it is.
    >>
    >> $hash{$v1} = 'better than just working!' ;

    >
    > Care to expound? What is dangerous about the first example? What's
    > with the "hash" in the second? As I understand it, there was no hash
    > involved - it was the name of a scalar variable stored as a string in
    > another scalar. Is hash a keyword or simply a variable name? If this
    > is a FAQ or a perldoc question, a pointer to the right location would
    > be appreciated. I don't find anything looking there and a Google for
    > $hash doesn't show up anything that seems relevant.


    Okay, I take it back, Uri.

    Dan:
    It is a FAQ; pointers to the FAQ are posted here regularly, and can
    be found by a quick google search, or (iirc) at rtfm.mit.edu.

    In short: using a hash instead of variable-names-as-a-variable is far
    superior for several reasons. The hash is neater (it's one data
    structure instead of multiple); it encapsulates the data in one neat
    package, and allows it to be hidden better within a subroutine or an
    object (insofar as Perl allows data to be hidden) (Encapsulation and Data
    Hiding are two important concepts in modern computer science); the
    variable-name approach can often lead to hard-to-find bugs due to typos
    and bad logic.

    No experienced Perl programmer uses variables as variable names
    except in those rare situations when no other approach will work.

    (Better, Uri? ;-) )
    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
    Eric J. Roode, Oct 12, 2004
    #16
  17. Jeff

    Uri Guttman Guest

    >>>>> "EJR" == Eric J Roode <> writes:

    EJR> Dan Jones <> wrote in
    EJR> news::

    >> Uri Guttman wrote:
    >>
    >>>>>>>> "EJR" == Eric J Roode <> writes:
    >>>
    >>> >> Is it possible?
    >>>

    EJR> Strange that you could figure out how to print the indirect
    >>> value EJR> out, but you couldn't figure out how to assign
    >>> indirectly. :)
    >>>

    EJR> ${$v1} = "it worked!";
    >>>
    >>>
    >>> strange that you should know better than to give a symref answer
    >>> without all the usual caveats on how dangerous and wrong it is.
    >>>
    >>> $hash{$v1} = 'better than just working!' ;


    EJR> Okay, I take it back, Uri.

    EJR> Dan:
    EJR> It is a FAQ; pointers to the FAQ are posted here regularly, and can
    EJR> be found by a quick google search, or (iirc) at rtfm.mit.edu.

    EJR> In short: using a hash instead of variable-names-as-a-variable is far
    EJR> superior for several reasons. The hash is neater (it's one data
    EJR> structure instead of multiple); it encapsulates the data in one neat
    EJR> package, and allows it to be hidden better within a subroutine or an
    EJR> object (insofar as Perl allows data to be hidden) (Encapsulation and Data
    EJR> Hiding are two important concepts in modern computer science); the
    EJR> variable-name approach can often lead to hard-to-find bugs due to typos
    EJR> and bad logic.

    EJR> No experienced Perl programmer uses variables as variable names
    EJR> except in those rare situations when no other approach will work.

    EJR> (Better, Uri? ;-) )

    better but not perfect!

    you should have hunted down all copies of your erroneous post and
    deleted them and burned all the disks that even carried it.

    :)

    and the rule for symrefs is simple. ONLY use symrefs when you NEED to
    mung the symbol table. they have no other purpose and regular hashes
    are better in every way for munging data.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Oct 12, 2004
    #17
  18. Jeff

    Uri Guttman Guest

    >>>>> "EJR" == Eric J Roode <> writes:

    EJR> Uri Guttman <> wrote in
    EJR> news::

    >>>>>>> "EJR" == Eric J Roode <> writes:

    >>
    >> >> Is it possible?

    >>

    EJR> Strange that you could figure out how to print the indirect value
    EJR> out, but you couldn't figure out how to assign indirectly. :)
    >>

    EJR> ${$v1} = "it worked!";
    >>
    >>
    >> strange that you should know better than to give a symref answer without
    >> all the usual caveats on how dangerous and wrong it is.
    >>
    >> $hash{$v1} = 'better than just working!' ;


    EJR> Aw, Uri, you're not letting me have any fun! ;-)

    EJR> Actually, I didn't feel it necessary, since others had already
    EJR> given good reasons not to do so (and to read the FAQ).

    but he may have read your post first. or someone may find it in
    google. if strict were on by default (other than for one liners) we
    wouldn't have this newbie use of symref syndrome.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Oct 12, 2004
    #18
    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. Bobby Chamness
    Replies:
    2
    Views:
    2,390
    Joe Smith
    Apr 22, 2007
  2. mfglinux
    Replies:
    11
    Views:
    699
    Roberto Bonvallet
    Sep 12, 2007
  3. Peter Buckley
    Replies:
    5
    Views:
    200
    matt neuburg
    Feb 27, 2009
  4. David Filmer
    Replies:
    19
    Views:
    240
    Kevin Collins
    May 21, 2004
  5. X l e c t r i c
    Replies:
    2
    Views:
    97
    X l e c t r i c
    Mar 2, 2008
Loading...

Share This Page