in reply to Variable expansion

Are you sure you're not literal quoting $cmd before putting it in the backticks?
my $hr = {foo => { bar => '/dev/null' } }; # will not work my $cmd = 'ls $hr->{foo}->{bar}'; print `$cmd`; # works a treat my $cmd = "ls $hr->{foo}->{bar}"; print `$cmd`; __output__ ls: -: No such file or directory /dev/null

HTH

broquaint

Replies are listed 'Best First'.
Re: Re: Variable expansion
by stephenpry (Initiate) on Apr 17, 2002 at 13:59 UTC
    I think the strings coming back are literals so what I must have is your case 1 which as you say doesn't work ! The literals contain strings that do map to local variables so somehow I've got to translate the part that is the variable to its value ? (I tried splitting it up where the '$' starts but when I set a new variable to: $newcmd=$cmd1.$cmd2; It seems to do the same thing.)
      If you do have the unfortunate case of having the string not being interpolated then you could use the string version of eval() to get the desired result.
      my $hr = { foo => { bar => '/dev/null' } }; my $cmd_literal = 'ls $hr->{foo}->{bar}'; my $cmd = eval qq(return "$cmd_literal";); print `$cmd`; __output__ /dev/null
      But the best option would be to have $cmd be interpolated rather than resorting to eval().
      HTH

      broquaint

        Brilliant ! That works, thankyou.. I've struggled all morning with this (in Scotland ..)