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

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

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 daffodils sway...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2017-12-15 15:15 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (433 votes). Check out past polls.