Problems? Is your data what you think it is? | |
PerlMonks |
Re: The 'eval "require $module; 1"' idiomby davido (Cardinal) |
on Apr 19, 2014 at 05:40 UTC ( [id://1082866]=note: print w/replies, xml ) | Need Help?? |
This is a generalized idiom, in the form of:
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
In Section
Seekers of Perl Wisdom
|
|