PBP: 105 Avoid string eval

The Best Practices suggest avoiding string eval.

This is, in my opinion, a good idea, although I may not be as absolute as many people who follow the perlcritic rule are.

In general, if you find yourself using string eval, I worry your design could be better.  Rather than trying to create functions on the fly with it, uese anonymous subroutines with closures.  They’ll be easier to debug, and will get you compile-time warnings instead of run-time errors.  This is better, and makes testing easier.

The PBP points out some other problems, too, such as most people forgetting to check the $EVAL_ERROR.

In general, it’s tricky and messy and there’s likely better ways to do it.  I avoid it, but assume that there might be places it might be the right answer.

Leave a Reply