Because there's no special characters in setenv X1 /p/fsd
,
system("setenv X1 /p/fsd")
is equivalent to
system("setenv", "X1", "/p/fsd")
It's failing because there's is no program named setenv
. If this optimisation didn't exist, you get a different result*. In that case,
system("setenv X1 /p/fsd")
would be equivalent to
system("sh", "-c", "setenv X1 /p/fsd")
That wouldn't work either because sh
has no setenv
command. So you could run the following instead:
system("tcsh", "-c", "setenv X1 /p/fsd")
But it would be pointless. You'd create a shell, set one of its variables. Then the shell exits and its variables cease to exist.
If you want to set a env var seen by Perl and its children, simply use
$ENV{X1} = "/p/fsd"; # Until end of program
or
local $ENV{X1} = "/p/fsd"; # Until end of scope
* — Optimised code shouldn't behave differently than unoptimised code, but since the optimisation just changes what error you get, it's acceptable.