jreisinger jreisinger - 4 months ago 7
Perl Question

Shell variable visible in double quoted Perl one-liner

I understand shell variables are local to the current shell while environment variables (the

export
ed ones) are passed onto child processes forked by the shell.

When I run a Perl one-liner within double quotes I can access the (local) shell variable from the
fork
ed perl process:

$ FOO=bar
$ perl -we "print qx'echo $FOO'"
bar


Why is that?

Answer

It's because of shell variable interpolation in double-quoted strings.

The $FOO variable is evaluated in the parent shell - in which $FOO has value bar - and interpolated into the quoted string.

Therefore the perl code you are actually running is:

print qx'echo bar'