Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any use cases where string eval is necessary in Perl?

Tags:

perl

eval

Can you provide any examples where using eval EXPR is really necessary? I'm asking because its generally discouraged.

like image 493
planetp Avatar asked Feb 23 '10 13:02

planetp


1 Answers

String eval is the only way to:

  1. execute new code in a runtime decided package
  2. add overloading to a package at runtime (at least according to overload.pm)
  3. execute arbitrary strings of Perl
  4. compile macro type substitutions

String eval should not be used to:

  1. evaluate symbolic references
  2. execute any code from the user
  3. trap errors
  4. if there is any other way to do it

eval can also be used to create ad-hoc communication protocols between different systems or different languages that also have eval (provided that everything is secure of course. JSON can be seen as a more secure, but limited subset of this method)

like image 195
Eric Strom Avatar answered Oct 21 '22 21:10

Eric Strom