Oh monks,

Here's the issue. I need to load a method conditionally, the condition being that a certain module Foo::Bar is usable. So after asking in the chatterbox, the following snippet arose:

eval "use Foo::Bar"; unless ($@) { sub my_method { # Code } }

So far so good. It worked for a trivial test subroutine, but not for my real-life example. The thing is, Foo::Bar = PDL. And PDL adds a lot of new syntax. Now, the eval expression is being executed at runtime, but the  my_method block has its syntax checked at compile time. So by the time that the syntax of the my_method is checked, PDL hasn't been loaded yet, and the module gives a syntax fatal error when the sub tries to

$matrix(0,0) = $value;
which is perfectly legal if PDL is loaded.
Also, I can't use  eval { use Foo::Bar }, because it dies on me if Foo::Bar isn't present.

I might be missing something here, but I surely don't know how to solve this. I would really appreciate some enlightenment!


In reply to Conditional loading of methods by bruno

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.