Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: eval "require $class" seems wrong

by rvosa (Curate)
on Aug 22, 2007 at 17:56 UTC ( #634453=note: print w/ replies, xml ) Need Help??


in reply to Re: eval "require $class" seems wrong
in thread eval "require $class" seems wrong

That also does a string eval, actually. See the _require sub in Module/Pluggable/Object.pm. Maybe I'm just barking up the wrong tree - I figured that something that's not uncommon (requiring a class at runtime, from a variable) could be done in a "best practices" sort of way. Apparently not. This seems strangely un-TIMTOWDI.


Comment on Re^2: eval "require $class" seems wrong
Re^3: eval "require $class" seems wrong
by ikegami (Pope) on Aug 22, 2007 at 19:08 UTC

    I was kinda suggesting the best practice was to avoid requireing dynamic class names completely, and address the problem at a higher level.

    To answer your question directly, one can require either a class supplied as a bareword or a path. There isn't a way to require a class supplied in a scalar. In order to load a dynamically constructed class name, some use eval EXPR to do the former (e.g. Module::Pluggable), and some transform the class name into a path and use the latter (e.g. if). I have no idea why one cannot do require $class;.

      The second paragraph summarizes nicely the whole thread, but I cannot agree with the best practice you are suggesting which is don't use it IIUC.

      eval-require is a common idiom for conditional loading of modules and there is a need for that. From a language perspective a special "use" would be better like 'use any LIST' as the intent would be much clearer. Still you don't want "use base/use parent" wars^Wdiscussions for something simply expressed in an idiomatic but inelegant way. In the end I think low-level things like these should be made core pragmas and (possibly) written in C, thriving for a minimalistic implementation.

      cheers --stephan

        eval-require is a common idiom for conditional loading of modules and there is a need for that.

        No, you don't need either eval functions to conditionally load a module.

        if (condition) { require Foo::Bar; }

        And you don't need eval EXPR* to do exception handling.

        eval { require Foo::Bar; }; if ($@) { require Foo::Baz; }

        You only need eval EXPR to load a module whose name is not known at compile-time. The only things I can think of that fits that description are plugins/drivers, things well served by high-level modules.

        Do you have an example that requires require $class other than plugins/drivers?

        * — Remember, nothing wrong with eval BLOCK (called try in some other languages). Unlike eval EXPR, it doesn't invoke the Perl compiler. That means it doesn't incure a huge speed penalty and it doesn't require careful escaping and validation its argument.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://634453]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-09-21 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (175 votes), past polls