In Perl, why do I have a syntax error when trying to use the eval string?

advertisements

Why isn't this working?

eval "$response = $ua->request($r);"
print "[email protected]";

gives:

syntax error at (eval 8) line 1, near "="


It isn't working because your double-quoted string is subject to interpolation, which is not going to go well, with those variables being expanded in place. And you need a semicolon outside your eval, not so much inside it. Try single quotes like so:

eval '$response = $ua->request($r)';