Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Creating dynamic variables

by Anonymous Monk
on Jan 03, 2013 at 10:29 UTC ( #1011434=note: print w/ replies, xml ) Need Help??


in reply to Creating dynamic variables

You could do it with anonymous subroutines. Just assume that every variable is a subroutine. It's not very pretty, though.

my %vars; # something to hold all your variables in sub set_var { my ($name, $value) = @_; $vars{$name} = $value; } sub get_var { my $name = shift; my $var = $vars{$name}; return ref $var eq 'CODE' ? $var->() : $var; } set_var(foo => "hello"); set_var(x => sub { return get_var('foo') . get_var('y') }); set_var(y => 'world'); print get_var('x'), "\n";


Comment on Re: Creating dynamic variables
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2015-07-29 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls