Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: The 'eval "require $module; 1"' idiom

by tobyink (Abbot)
on Apr 19, 2014 at 09:14 UTC ( #1082876=note: print w/ replies, xml ) Need Help??


in reply to The 'eval "require $module; 1"' idiom

I generally consider it to be cargo cult. There's no situation where require lives but returns false.

That said, your case 3 is incorrect. Require doesn't (always) return "1". It returns whatever the module returned.

$ cat Foo.pm package Foo; 2; $ perl -E'say eval "require Foo"' 2

Case 3b is correct though - if a module has already been loaded, require just returns 1.

$ perl -E'say eval "require Foo" for 1..3' 2 1 1
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name


Comment on Re: The 'eval "require $module; 1"' idiom
Select or Download Code
Re^2: The 'eval "require $module; 1"' idiom (one character cargo cult)
by Anonymous Monk on Apr 19, 2014 at 09:25 UTC

    I generally consider it to be cargo cult. There's no situation where require lives but returns false.

    So the threshold for "cargo cult programming" is one character?

    I don't think you can ever determine if this idiom is caro-cult based on this idiom, especially in isolation

      As per your own link, cargo cult programming is the ritual inclusion of code or program structures that serve no real purpose. This is code that serves no real purpose. I don't have a character threshold. If it were possible to include code or program structures using zero characters, and that code or structure served no purpose, then I'd argue that to be cargo cult too.

      This kind of thing is what gives newcomers to Perl the impression that it's full of magical incantations, and they'd be better off using Python. (I grant you that this is a very minor and seemingly insignificant example, but it does all add up!)

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

        As per your own link, ...omit the essence that is ritual inclusion without understanding../

        Cute:)

        How is one character a ritual? How is using it inclusion without understanding?

Re^2: The 'eval "require $module; 1"' idiom
by sedusedan (Monk) on Apr 19, 2014 at 11:56 UTC
    Yup, I miswrote. Case 3 is when the module returns true and likewise require returns true too (which is whatever true value that the module returns). Thanks for clearing that part up.
Re^2: The 'eval "require $module; 1"' idiom (intended)
by tye (Cardinal) on Apr 19, 2014 at 18:54 UTC
    I generally consider it to be cargo cult.

    I intentionally put the "; 1" into this exact construct not out of some worship of practices that I have observed and not understood.

    I put the "; 1" to show that I am explicitly checking for whether or not the code inside of the eval dies.

    To a lesser extent, I also do it just to short-circuit the reader having to contemplate if there are any ways where require could not die but also not return a true value. I believe that the 'die' behavior of require is quite well understood.

    However, the return value from require is almost never used. It isn't even really documented. You can infer likely return value behavior from Perl code included in the require documentation but that code is marked "Has semantics similar to the following subroutine:" (emphasis added), so such conclusions seem wise to treat with a certain level of skepticism.

    The primary interface for require is that it loads the module or it dies. So it makes sense to use a construct that tests for whether or not the require died. That is what the "; 1" does. Leaving off the "; 1" requires the reader to try to find and understand information about a bit of behavior that is nearly useless and is poorly documented. That is not an improvement.

    - tye        

      ++

      ...which is why, as an idiom, it's wise to disconnect the success flag from the return value of the thing_that_may_fail(). I'm glad I'm not alone in seeing merit in the clarity that comes from decoupling the thing that may fail from the idiom that tests it.

      The point to "eval "possible_failure(); 1"" is that we're not concerned with possible_failure()'s return value, we're concerned whether or not it threw an exception. We shouldn't have to look up and infer what its return value will be, we should simply test whether it threw an exception or not. That's where 1;" or warn "Woops!"; comes in.


      Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-07-29 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls