Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Testing for a module's presence

by Tanktalus (Canon)
on Feb 06, 2005 at 05:26 UTC ( [id://428428]=note: print w/replies, xml ) Need Help??


in reply to Re: Testing for a module's presence
in thread Testing for a module's presence

We take some risks here either way. "perl" may not be the currently running perl (here, for example, "perl" is the stock perl that comes with the Linux distro I'm running - 5.8.0, while "perl5.8" is a symlink to the latest perl5.8 binary I've compiled - 5.8.5), while using $^X may also not be quite useful (since the currently executing code may be running in an embedded perl rather than a standalone perl executable - isn't that how mod_perl works?).

Personally, I'd just use require as others have pointed out. And if you don't want to use the extra memory, you can delete it from %INC afterwards - perl will then be able to re-use that memory. It does mean that you'll get a negative when the module exists and is found, but doesn't compile, but that's probably the same thing as not being there, really.

Replies are listed 'Best First'.
Re^3: Testing for a module's presence
by sgifford (Prior) on Feb 06, 2005 at 05:59 UTC
    Will deleting from %INC really free much memory? For example, will it free storage space for strings and subs created by the module when it's required? Or does it just free the storage for the filename itself?

      You're probably right ... which is why a later response pointed out removing some of the global symbol table. I'm hoping someone who knows the guts of perl better can help out there, but meanwhile, searching @INC is definitely safer and faster and uses less memory :-)

Re^3: Testing for a module's presence
by Anonymous Monk on Feb 06, 2005 at 05:34 UTC
    And if you don't want to use the extra memory, you can delete it from %INC afterwards - perl will then be able to re-use that memory.

    Memory is an issue! Can you show me how to delete it from %INC after I required the module in an eval? Thanks.

      If memory is an issue, traversing @INC may be your best option. Anyway, to answer the question - assuming you're dealing with Some::Module, you would simply delete $INC{'Some/Module.pm'} although you may also need to delete it from the overall symbol table: undef ${Some::Module::} - that last part is untested, and I'm sure someone will correct me with the right way to do that. If it's possible. Which is why I go back to traversing @INC as your most memory-conscious choice, although because you won't be compiling it, its mere presence, even if it doesn't compile, will give you a different answer (probably not important, but I like to keep all the risks, however small, on the table, in case they are not small to you).

        If memory is an issue, traversing @INC may be your best option.

        So if I used File-Find to traverse @INC, looking for Some::Module, that would be faster and more efficient that using a require within an eval? I keep thinking that if I have over 1,000 modules on my system, I will be doing a lot time-consuming searching. Am I wrong to look at it this way?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-24 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found