in reply to The 'eval "require $module; 1"' idiom
This is a generalized idiom, in the form of:
eval "something_that_might_fail(); 1;" or die "Message.";
In the general case, it makes sense because "something_that_might_fail()" could be some expression where "false" or "undef" are not out of band for success (ie, are perfectly valid return values even for a successful call).
In the specific case of "eval "require Some::Module; 1;" or die "Message.", your "Case 3" and "Case 3b" observations are accurate; require will return 1 on success. In this specific case, the 1; is redundant, though semantically it does maintain eval's return value to not be tied to the return value of the primary "thing that might fail."
On the one hand, one might say that "eval "require Foo; 1;" or die "Message";" is cargo culted. On the other hand, the idiom as a general tool is useful, and probably doesn't need to be optimized further just because this specific application doesn't need the 1;. It's healthy to decouple the thing that might fail's return value from the success sentinel, which is what the idiom does.
By the way; this is a very well researched question. Kudos.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: The 'eval "require $module; 1"' idiom
by sedusedan (Pilgrim) on Apr 19, 2014 at 12:02 UTC | |
by davido (Cardinal) on Apr 19, 2014 at 17:04 UTC | |
by DouglasDD (Beadle) on Aug 09, 2019 at 12:45 UTC |