in reply to Re: problem with string literal in system()
in thread problem with string literal in system()

With the given contents of $cmd, that expression won't compile, so $cmd will be undef. If you want templating functionality, use a templating system! Using eval EXPR is such a bad idea.

Replies are listed 'Best First'.
Re^3: problem with string literal in system()
by cephas (Pilgrim) on Sep 22, 2006 at 03:23 UTC
    Its not trying to execute $cmd. Its wrapping $cmdwith "'s, and then eval'ing. It will return a string with the values interpolated.

    Update: ikegami is correct, the way the command is layed out, it will generate an error. I thought ikegami was trying to point out that the command itself wasn't valid perl, not that it was building an invalid string (that is bad perl). The point of the story is that interpolation doesn't run arbitrarily deep, if you want another layer, you'll have to eval, and get all of the problems that go along with it.
      You forgot to take into account that $cmd contains double quotes. Why didn't you try it?
      use strict; use warnings; my $cmd = '/opt/sudo/sudo /usr/bin/su - $ENV{LOGNAME} -c "$someScript" +'; $cmd = eval "\"$cmd\""; print($cmd, "\n");

      outputs

      Scalar found where operator expected at (eval 1) line 1, near ""/opt/s +udo/sudo /usr/bin/su - $ENV{LOGNAME} -c "$someScript" (Missing operator before $someScript?) String found where operator expected at (eval 1) line 1, near "$someSc +ript""" (Missing operator before ""?) Use of uninitialized value in print at 574310.pl line 5.
      A reply falls below the community's threshold of quality. You may see it by logging in.
      Thanks for the suggestions cephas and ikegami. I did the below:
      my $testCmd = (split /\|/, $configLine)[1]; my $cmd = eval "qq($testCmd)"; my $activity = "testActivity"; system("$cmd 1>$activity.stdout 2>$activity.stderr");
      I noticed that it was giving an UnInitialized variable message when I tried to  print "$cmd\n"; And, it executed the command. Thanks again