Performance questions such as these can be answered empirically by using Benchmark. The accompanying POD describes its use.
The theory behind the performance difference will involve the fact that the string that you're evaling must be compiled at runtime, which will take a little time. Whether or not the time it takes is significant to your script's performance is another question. More often than not, the answer is ...not really. But you would have to profile a time-critical script to determine where the bottlenecks are before digging into how to optimize speed performance.
Common uses for eval strings are varied. One is to build a regexp on the fly. Another is to bring back to life something that was previously dumped via Data::Dumper (though the eval is often done behind the scenes via the same module).
And of course, any time that you find you really don't know how the code is supposed to come together until runtime string eval comes in handy (the regexp-on-the-fly is just one example).
For the most part, you won't need string eval, except when you really need it, and in those cases, it will seem natural (though it can be tricky getting metacharacters and quoting right).
eval is also useful, of course, for trapping errors, though often that's done using the block version of eval rather than the string version.
There is a really good chapter on eval in Advanced Perl Programming, published by O'Reilly & Associates (known as the Cougar book). It gives a few great examples.
Dave
In reply to Re: eval string possibilities
by davido
in thread eval string possibilities
by erix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |