Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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 ''"; print "Debug: MODULE_WAS_LOADED=$MODULE_WAS_LOADED\n";

And in

# ... 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
Replies are listed 'Best First'.
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 ''";

    Or perhaps even:

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

    (which might look nicer in the debugging output).

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2015-11-29 17:54 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (751 votes), past polls