Turbo Sullivan Turbo Sullivan - 1 year ago 93
Python Question

perl eval with back quote

I was converting a perl script into python and then I found this function which I couldn't understand.

sub set_xyz_hash {
%xyz = ();
$_ = join(' ', map { my $a = $_; $a =~ s/\'/\'\"\'\"\'/g; "'$a'" } @ARGV);
eval `XYZ_ARG0='$0' NZENV_OUTPUT='-a -p xyz' xyz $_`;

since my knowledge in perl is very limited, I couldn't understand 'eval' used in this function. can someone please help me here?

note, eval is used wth ` (back quote).

Answer Source

The backticks run an external command and return the output:

my $result = `ls -l`;
print $result;

Your eval line is processed exactly the same way:

my $result = `XYZ_ARG0='$0' NZENV_OUTPUT='-a -p xyz' xyz $_`;
eval $result;

It's basically fetching some Perl source from the called script and runs it.