views:

300

answers:

3

Why isn't this working?

eval "$response = $ua->request($r);"
print "$@";

gives:

syntax error at (eval 8) line 1, near "="
+12  A: 

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)';
chaos
Why not curly braces?
innaM
While answered, it is important to note, as the previous commenter alluded to, that stringy evals that could otherwise be accomplished by block evals, mentioned below, are suspect. Please modify your answer lest we start advocating the use of stringy evals.
Mark Canlas
I don't think I should modify my answer, as Brad Gilbert has a perfectly good alternative one, but I do want to say that I am not advocating stringy evals; I was just trying to correct the immediate, syntactical deficiencies in OP's code, where Mr. Gilbert addresses the tactical deficiencies.
chaos
chaos: I think Mark is saying that 'best' answers should contain the best answer, which in this case is block eval "when possible" (and include the eval {...; 1} construct). That, I believe, is how StackOverflow is meant to work. If you don't edit, some editor can, should, and will.
runrig
Accepted answer and best answer are different concepts.
chaos
This answer **should** be the accepted answer. It actually answers the question. While my answer, just points out another option.
Brad Gilbert
+18  A: 

A better question is why you are using a string eval, instead of a block eval?

eval { $response = $ua->request($r); }
print "$@";
Brad Gilbert
Better yet, catch the exeption with an "eval or do" construct: eval { blah; 1} or do { warn "Eval failed: $@" };
daotoad
+2  A: 

An even better better question is why you are using eval in the first place? I suspect that you are using LWP::UserAgent and unless you implement your own request object, the 'request` method is unlikely do die.

Thus, why not simply use:

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

?

innaM
because that line of code is dying and causing a internal server error when I try to execute it on an https request.
@Manni +1. @OP then figure out how not to die.
Sinan Ünür
What Sinan was trying to say in his charming way was: Wrapping dying code in an eval won't make the code work.
innaM
@Manni I was typing fast ;-) For anyone who comes across this thread, the way to avoid dying on an https request is to read the README.SSL document that comes with LWP. See also http://stackoverflow.com/questions/1228572/other-way-to-send-https-request-in-perl-without-lwpuseragent-and-httprequest/1228730#1228730
Sinan Ünür