in reply to Re: Gracefully choosing which module to use
in thread Gracefully choosing which module to use

UNIVERSAL::require does not let us avoid an eval, unless you mean avoid doing the eval ourselves. It precisely does eval "CORE::require $module".

Replies are listed 'Best First'.
Re^3: Gracefully choosing which module to use
by nothingmuch (Priest) on Jun 14, 2005 at 10:55 UTC
    Yeah, I know it's guts. But good point =)

    What I meant is that you don't have to construct some perl code, eval it, and maybe get it wrong and stuff.

    Encapsulation let's me feel free to ignore most of what's underneath, but normally before I get to that stage I take a good look at the insides to make sure I can trust it.

    -nuffin
    zz zZ Z Z #!perl
      The only thing it does different from what I would have naively done before I read its source code is to call CORE::require instead of require. But such streaks of paranoia are only necessary in a world where people do things like putting code in UNIVERSAL, so... :-)

      PS: Yes, I know it does that not out of paranoia but rather out of having a sub named "require" in it's own namespace. I was joking.

        Ah, but that is not the reason I use UNIVERSAL::require.

        In an effort to optimize for readability, I try to make sure that there are no bits of code that don't make perfect sense when being read.

        Ofcourse, with some lines this just can't be done, but then you use comments.

        The point is that eval "require $class"; is readable, but to when I read the code eval is one of the things that usually makes me pay close attention, and if this is only loading the module in $class, It's nothing special.

        Loading a module is not what the lines where it's loaded are doing, but rather to what those lines need to do whatever it is they do.

        By that rationale I prefer to make that code stand out less, so that I can switch focus to the important code more quickly.

        I know this might sounds a bit far fetched, but I tend to have a hard time concentrating when I'm reading, unless I'm in a very good state (happy, energized, not hungry, not thirsty, not in a rush, not too jumpy, not too distracted...

        -nuffin
        zz zZ Z Z #!perl