Care to explain why I wouldn't want to use eval in this case?

Mostly it's instinct. Using eval mixes up program space a dataspace even more than symrefs. Where there are two solutions of equal complexity one using eval(STRING) and the other not then use the one without.

The other nasty thing about eval is that unless you use a #line directive the errors you get just say "in eval 666" or something equally unhelpful.

The example doesn't show but I use eval's result to see if all went well (the variable names and values come from a db that might have garbage in them, causing compilation errors).
But if you didn't constuct a string as pass it to the compiler then there wouldn't be any errors! Why should garbage in the values cause errors? As it happens garbage in the names won't throw any errors either since symbols defined with symrefs can have any name.

Anyhow if the database is not trusted to be valid you definitely should not be passing it to eval() in case it decides to reformat your disk.

You are praising eval(STRING) because it provides you with a solution that it caused.

With my solution all you need to add is a check that $varName !~ /\W/. Actually if there's a chance that the database is truely malicious and not just erroneous then you really should check $varName is not null or one of the special globally global variables.

$varName && $varName !~ /\W/ && \*$varName != \*{"::$varName"}

In reply to Re^3: Dynamic USE and %INC by nobull
in thread Dynamic USE and %INC by Bas-i

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.