On 4/3/06, Dr.Ruud <[EMAIL PROTECTED]> wrote:

> my %table;
> eval '$table{' . $_ . '} = \&' . $_
>    for qw(cat jackalope yeti);

myeyesmyeyesthegogglesdoNOTHING

When I posted my code, I said that I wrote it that way to avoid using
"the dreaded soft reference". But using a soft reference is far better
than using the abominable and truly wretched text eval.

Why is text eval so bad? Because it can accidentally turn data into
code. Beginners, especially, should be cautioned against text eval.
It's worse than using goto: goto can only branch to a label, but text
eval can do anything Perl can do.

If you truly must use the text eval, minimize it:

  $table{$_} = eval '\&' . $_
    for qw{ cat jackalope yeti };

But using the soft reference is a big step up, because it can't
accidentally turn data into code:

  {  no strict 'refs';
    $table{$_} = \&$_ for qw{ cat jackalope yeti };
  }

In the end, yes, your code works, for sufficiently small values of
"works". But the text eval is a dangerous and powerful beast, not
easily tamed. Avoid avoid avoid.

Cheers!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to