From: Kripa Sundar <[EMAIL PROTECTED]>
   Date: Sun, 14 Aug 2005 18:15:08 -0400

   Dear fellow mongers,

   I saw a friend using an eval() where it was unnecessary.  Of course,
   eval() is a valuable tool, with many valid uses.  But in this specific
   instance, it was truly unnecessary, and so I advised him to eliminate
   the eval().  (The code he used was "eval $string", where the value of
   $string was known at compile-time.)

   . . .

   I could think of three problems with eval(), and replied to him with
   the three points below.  Please let me know if I have gotten anything
   wrong below.  Also, if you can think of more eval() concerns, please
   help me learn.  TIA.

My favorite brief against this form of eval is that it obscures the
control flow, and this case sounds like an excellent illustration.
Rather than:

        my $string = join("\n",
                          'do_something($to_something);'
                          '$some_other_thing = do_something_else();');
        eval $string
        if ($@) ...

I would much rather write:

        eval {
            do_something($to_something);
            $some_other_thing = do_something_else();
        };
        if ($@) ...

In fact, I wouldn't even swear I understood how $to_something and
$some_other_thing are scoped in the first case.  (Not that I believe
I'll ever need to know.)

                                        -- Bob Rogers
                                           http://rgrjr.dyndns.org/
 
_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to