I have a class with MANY internal variables, and I am trying to write a single get method that will access any one of them, where I pass in a string that contains the variable name(security is not an issue). Basically, I am trying to be lazy. My broader goal is to encapsulate the parsing of an external file so that when the format of that file changes, I only have to change the parser and not other parts of my code. I have been unable to get the evaluation to work. My NON-WORKING code looks like this:
package parser; my $var1; my $var2; my $var3; ... #set values -- this part works sub parse{ ... $self->{$var1} = 'a'; $self->{$var2} = 'b'; $self->{$var3} = 'c'; ... } #get values -- doesn't work. $var_name is a string such as 'var1', # so would be called as get_var('var1'). My goal is to get # back the value of $var1 sub get_var{ my ($self, $var_name) = @_; return $self->{$$var}; #also tried double quotes, etc. }
I would be very grateful for enlightenment
In reply to trouble with evaluate by genghis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |