Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Modules return value

by cavac (Vicar)
on Jan 17, 2023 at 14:47 UTC ( #11149644=note: print w/replies, xml ) Need Help??


in reply to Re: Modules return value
in thread Modules return value

No, I can't think of any reason why you'd want to return a false value.

I can. Say you write a Parser to handle XML files. You might check during a BEGIN() if there is any functioning backend you can use. Yes, your module probably did some kind of test during install time, but that doesn't mean that those external libraries are still available and still functioning.

Now, why would you not want to just crash-and-burn or die()? Maybe your library is used from another module that's also dynamically searching for usable XML backends. There are many ways to load a ".pm" file (require, eval, probably Inline::Perl, ...), not just "use", and the easiest way to check if the operation worked is if you get a true value back. TIMTOWTDI, and yes that probably includes all the stuff that source code filters could do for (or "to") you ;-)

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

Replies are listed 'Best First'.
Re^3: Modules return value
by kcott (Archbishop) on Jan 17, 2023 at 17:18 UTC

    G'day cavac,

    While I don't necessarily disagree with anything you wrote there, I'm wondering if we're talking at cross-purposes.

    In the past, I've certainly written modules which, as part of loading, attempt to include some other module whose name is determined dynamically (perhaps based on environment, config, or similar). I would expect failure to be handled a little more elegantly:

    Some/Module.pm failed to load because ...

    Rather than what you refer to as "crash-and-burn":

    Some/Module.pm did not return a true value ...

    Could you provide some skeleton code that clarifies the scenario you're describing? Thankyou.

    — Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2023-04-01 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?