Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by Anonymous Monk
on Apr 19, 2014 at 23:51 UTC ( #1082925=note: print w/ replies, xml ) Need Help??


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

As plenty of monks have pointed out, in the context of a conditional the ;1 isn't needed.

In other contexts, consider:

my $MODULE_WAS_LOADED = eval "require 'bar.pl'"; print "Debug: MODULE_WAS_LOADED=$MODULE_WAS_LOADED\n";

And in bar.pl:

# ... module code here ... "For some reason this module has chosen to make its return value some ridiculously long value; perhaps the original author simply forgot to add the usual 1; at the end of the module and this return value is simply whatever the last statement in the module evaluates to. Whatever the case, I'd prefer it not polluting my debug output." #EOF


Comment on Re: The 'eval "require $module; 1"' idiom
Select or Download Code
Re^2: The 'eval "require $module; 1"' idiom
by tobyink (Abbot) on Apr 20, 2014 at 02:20 UTC

    Even in that case, I still wouldn't add ";1" to the evaluated string. In your example, use warnings would be complaining about concatenating an undefined value. Instead I'd probably go with

    my $MODULE_WAS_LOADED = !! eval "require 'bar.pl'";

    Or perhaps even:

    my $MODULE_WAS_LOADED = 0+!! eval "require 'bar.pl'";

    (which might look nicer in the debugging output).

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2014-10-02 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (61 votes), past polls