in reply to Re: Re: possible to evaluate scalars stored in a string?
in thread possible to evaluate scalars stored in a string?
What you need is eval, BUT 'command $foo' is unlikely to be valid perl so the eval will fail. This regex method works because we capture the scalar and perform the required (double) eval (first eval gets you $var out of $1, second gets you 'value' out of $var). You may wish to add more than \w in the capture after the $ for things like $foo->{bar} if you are getting that weird.
This is almost certainly a bad way to do it BTW.%hash = ( 'command' => 'command_name -n $var1 -p $var2' ); $var1 = 'foo'; $var2 = 'bar'; $command = $hash{'command'}; $command =~ s/(\$\w+)/$1/gee; print $command; # system ($command); __DATA__ command_name -n foo -p bar
cheers
tachyon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: possible to evaluate scalars stored in a string?
by etcshadow (Priest) on Dec 13, 2003 at 05:03 UTC | |
by tachyon (Chancellor) on Dec 13, 2003 at 05:07 UTC |