Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Re: Runtime Hash Variable access

by Juerd (Abbot)
on Mar 08, 2004 at 14:28 UTC ( #334820=note: print w/replies, xml ) Need Help??

in reply to Re: Runtime Hash Variable access
in thread Runtime Hash Variable access

Note also that symbolic references like this are very bad style and disallowed by strict 'refs' for a very good reason. If you read a good book (like Beginning Perl), you learn that a hash of hash references is much better than accessing global hashes by variable symbol names.

I don't think the OP knows Perl and think any help is a waste if they don't read a good book or tutorial. This site has many resources that can help them, most of which can be found on the Tutorials page.

Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

Replies are listed 'Best First'.
Re: Re: Re: Runtime Hash Variable access
by muba (Priest) on Mar 08, 2004 at 14:39 UTC
    What I was doing in my reply is called a sumbolic reference?

    $the_var_i_want = "this is the actual value I want to get"; $the_name = <STDIN>; #say, this would be "the_var\n" chomp $the_name; print ${"${the_name}_i_want"};
    And that is deprecated? Why is that? I think it's quite a nice way of getting the value of a variable which name you don't yet know.

      What I was doing in my reply is called a sumbolic reference?

      Symbolic, yes.

      And that is deprecated?

      No, just frowned upon.

      I think it's quite a nice way of getting the value of a variable which name you don't yet know.

      It's not. It requires global variables, which suck (see Coping with Scoping) and makes debugging hard. Instead, just use a hash (forget for a moment that the stash is a hash, if you knew that). Your code, using a hash and strict, would be:

      use strict; my %hash; $hash{the_var_i_want} = "this is the actual value I want to get"; my $the_name = <STDIN>; #say, this would be "the_var\n" chomp $the_name; print $hash{"${the_name}_i_want"};
      See also Why it's stupid to `use a variable as a variable name'.

      Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

      What Juerd tries to explain, IMVHO, is:

      If you feel the need to access variables whose names are unbeknownst to you, you should use a hash, whose name is known to you, with these unkown names as keys.

      There are very few situations where it's considered good design to use symbolic references (e.g. on-the-fly generation of objects implementing a known interface), and accessing variables is almost never one of them.

      Just my $0.02...


      Hlade's Law:

      If you have a difficult task, give it to a lazy person --
      they will find an easier way to do it.

      It is not deprecated, it is not allowed by strict 'refs'; these are different things. Of course, since you are strongly advised to use strict (which includes strict 'refs') as a default, you might say that symbolic references are deprecated. As to why symbolic refs are discouraged, the reasons are that (a) they don't work with variables declared with my -- which will usually be the majority of the variables you use if you use strict, and (b) their functionality is provided by mechanisms that don't require you make assumptions about your program's runtime environment (you won't always know that the variable you're trying to use as the name of another variable really is). See for a short account of why you should use strict, and perlref for the story about symbolic references.

      You are of course allowed to do it, if you know what you are doing (by disabling strict 'refs' in a code block where you do that. But if you ever find yourself tempted to do this, ask yourself whether there's a way around it first. As an illustration of point (a), the following version will not do what you want it to do:

      #!/usr/bin/perl use warnings; my $foo = "what?"; my $bar = "foo"; print $$bar;
      (if you run it, you'll see that you get an "uninitialized value" warning on the print line) -- because that $$bar is supposed to point to $main::foo, which is distinct from my $foo.

      To go to your example, why not code it with a hash?

      my %vars = ( the_var_i_want => 'this is the actual value' ); chomp (my $the_name = <STDIN>); print $var{"${the_name}_i_want"};

      The language provides many tools for working with the contents of hashes. You can put all the variables you need to access based on runtime values in hashes, and get all the functionality you get with symbolic references; with hashes, you can even easily find similar names, for example.

      If not P, what? Q maybe?
      "Sidney Morgenbesser"

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://334820]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-10-04 22:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found