I see your code as similar to rewriting:

if( something ) { do_this(); } else { do_that(); }
as eval( "do_th" . (something?"is":"at") . "()" ); Sure, you can make that work. But what is the point? Stringy eval has to recompile code every time it is run. Unless you are very careful, it can hide serious errors (and you weren't careful in your original code and neither was I in any of the code using eval that I posted in this thread). It can leak memory.

I just don't see any point to you using stringy eval here. I only use stringy eval as a last resort. Reaching for that tool too quickly is a common mistake when learning Perl so most experienced Perl programmers discourage it. It is easy to use stringy eval incorrectly.

Ok ... so what I was missing was the {}.

Huh? I didn't add any braces. The main difference between your use of eval and mine was that you expanded the value of $_[0] (by using double quote) while I passed eval the literal string '$_[0]'. I left the {} out of the non-eval code since I was using $ref and not $_[0] so the braces aren't needed.

That would fail if you put use strict 'refs' in the eval, right?

No, my code was using hard reference (not symbolic references) and so use strict 'refs' wouldn't have a problem with it (except, perhaps, for the GLOB dereferencing; I don't use GLOBs as references so I don't track the details of doing that). Your code would only work if $_[0] contained a variable name, causing the eval to use symbolic references.

        - tye (but my friends call me "Tye")

In reply to (tye)Re3: Eval and $@% ... by tye
in thread Eval and $@% ... by dragonchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.