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.

%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
This is almost certainly a bad way to do it BTW.

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
    Well, it's perfectly possible and reasonable to do it with eval:
    %hash = ( 'command' => '"command_name -n $var1 -p $var2"' ); $var1 = 'foo'; $var2 = 'bar'; $command = eval $hash{'command'}; print $command;
    Just changed it so that what's being eval'd is a valid perl expression that evaluates to what he wants.

    ------------
    :Wq
    Not an editor command: Wq

      ++ I remebered there was a double quoting way to do it but could not remeber exactly what and make it work.....

      cheers

      tachyon