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

Creating dynamic variables

by rbala (Acolyte)
on Jan 03, 2013 at 09:48 UTC ( #1011428=perlquestion: print w/replies, xml ) Need Help??
rbala has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I have a small query regarding variable assignments in perl. See the below code.
$foo = "hello"; my $x = "$foo".$y; # $y is not yet defined. $y = "world"; print $x;
This gives output as only hello, seems the variable $y is not dynamically substituted in $x based on the next assignment $y = world. Is there any way to accomplish this like in makefiles we have this type of assignments. I got this situation when i want to create files dynamically with different names using this type of substitution. Any help ???? Thanks in Advance!!!

Replies are listed 'Best First'.
Re: Creating dynamic variables
by Corion (Pope) on Jan 03, 2013 at 09:52 UTC

    Maybe you want a templating system instead? sprintf is the simplest one, and it could be used in your case as follows:

    $foo = "hello"; my $x = "$foo%s"; my $output = sprintf $x, $y; # replaces "%s" with $y print $output;

    There are many, many more templating systems, with varying easy of use and complexity. I recommend looking at simple string substitution first.

Re: Creating dynamic variables
by flexvault (Monsignor) on Jan 03, 2013 at 10:59 UTC

    Hello rbala,

    I'm guessing, but is this what you want to do?

    perl -e '$x="hello "; while( $y=glob("*") ) { print "$x$y\n"; }'

    If you had 'use strict' in your code, you would have gotten an error on your second line. I'm using 'glob', but any source of input and assignment of '$y' will work.

    When using Perl, you should think of how to do it with Perl. Your experience with 'makefile' is nice, but has nothing to do with what is correct in Perl!

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

Re: Creating dynamic variables
by tobyink (Abbot) on Jan 03, 2013 at 15:06 UTC

    This is about the closest I could get...

    use strict; use warnings; BEGIN { package JIT; # Just In Time evaluation $INC{''} = __FILE__; use base 'Exporter'; our @EXPORT = 'jit'; sub jit (&) { bless $_[0] } use overload q[""] => 'evaluate', q[0+] => 'evaluate', q[bool] => 'evaluate', fallback => 1; sub evaluate { $_[0]->() } }; use JIT; my $foo = "hello"; my $y; # needs to be declared, but we haven't assigned a value to it +yet. my $x = jit { "$foo".$y }; # $y is not yet defined. $y = "world"; # now define $y print $x;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Creating dynamic variables
by Anonymous Monk on Jan 03, 2013 at 10:29 UTC

    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";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011428]
Approved by marto
and a moth chases the moon...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2017-08-21 03:28 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (317 votes). Check out past polls.