Capture variable assignments in a Perl eval

Posted by Bruce on Stack Overflow See other posts from Stack Overflow or by Bruce
Published on 2011-11-23T23:28:20Z Indexed on 2011/11/24 1:51 UTC
Read the original article Hit count: 148

Filed under:

I would like to be able to capture variable assignments from a Perl eval. That is, to determine what variable names have been assigned to within the code and extract their value.

For example if I run:

eval '$foo=42; $bar=3.14;'

The result of the eval is 3.14 (the last value evaluated), but I would also like to be able to determine the names "$foo" and "$bar" and their values (without knowing the names in advance).

I have read up on a couple of ways of inserting variables into the eval block, through Safe and Eval::Context, but not yet any way of extracting them. I am more familiar with Python's eval/exec which have built in support for this.

© Stack Overflow or respective owner

Related posts about perl