I just wanted to make sure that I understood what was going on here...

(these are set up for a win32 shell, change your quotes to single ticks to run on UNIX...)
perl -e "print eval qq/require DBI; DBI::SQL_VARCHAR/ || $@;" >>> prints 'DBI::SQL_VARCHAR' perl -e "print eval qq/require DBI; DBI::SQL_XVARCHAR/ || $@;" >>> prints 'DBI::SQL_XVARCHAR' perl -e "print eval qq/require DBI; DBI::SQL_VARCHAR()/ || $@;" >>> prints '12' perl -e "print eval qq/require DBI; DBI::SQL_XVARCHAR()/ || $@;" >>> prints 'Can't locate auto/DBI/SQL_XVARCHA.al...' perl -e "$it=q/SQL_VARCHAR/; print eval qq/require DBI; DBI::$it/ || $ +@;" >>> prints 'DBI::SQL_VARCHAR' perl -e "$it=q/SQL_VARCHAR/; print eval qq/require DBI; DBI::$it()/ || + $@;" >>> prints '12' perl -e "$it=q/SQL_XVARCHAR/; print eval qq/require DBI; DBI::$it/ || +$@;" >>> prints 'DBI::SQL_XVARCHAR' perl -e "$it=q/SQL_XVARCHAR/; print eval qq/require DBI; DBI::$it()/ | +| $@;" >>> prints 'Can't locate auto/DBI/SQL_XVARCHA.al...'
Since I'm using an eval "" instead of an eval {}, it's a run-time evaluation (and by that I mean that it's parsed at runtime, whereas eval {} has the opportunity to parse some/all of your eval {} statement at compile time).

So, if eval "" finds something in there that doesn't look like a function call or a variable, it appears to treat it as a raw string. (The '()' at the end of the constant will make it look like a function)

I kinda expected it to complain about a bareword, or to actually go resolve the bareword as a constant, though. My only insight to this, is that eval() translates barewords into strings themselves, instead of being left as barewords. In other words,
eval "DBI::SQL_VARCHAR"; # is the same as $it='DBI::SQL_VARCHAR'; eval { $it };
Am I off-base here? Is that the way it works, or is something else going on here?

This seems like a case where DWIM didn't seem work for me... ;)

-Dave

In reply to runtime eval() and constants by dpmott

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.