Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Can I see "requires"?

by tobyink (Canon)
on Mar 08, 2012 at 15:47 UTC ( [id://958486]=note: print w/replies, xml ) Need Help??


in reply to Can I see "requires"?

As others have already said, %INC.

However, note that it's quite possible to directly manipulate this hash:

use 5.010; # Trick perl into thinking that Moose.pm has # already been required. $INC{'Moose.pm'} = '/tmp/Moose.pm'; # Require it for real. But perl does nothing! require Moose; # Perl thinks Moose is loaded. say "Moose is loaded" if $INC{'Moose.pm'}; # But it isn't really, so we don't know VERSION. say 'Moose version:', Moose->VERSION;

I have in the past had cause to directly manipulate %INC. A module I was using wanted to be passed the name of plugin module to use. It would require that plugin and then use it. However, I wanted to define the plugin inline in my code, a la

{ package My::Plugin; ... } Something::That::Uses::Plugins->load_plugin('My::Plugin');

But this caused Something::That::Uses::Plugins to attempt to require the non-existent file "My/Plugin.pm". So...

{ package My::Plugin; ... $INC{'My/Plugin.pm'} = __FILE__; } Something::That::Uses::Plugins->load_plugin('My::Plugin');

And everybody's happy.

But anyway, my point is that although %INC should give you the information you want, it can't necessarily be trusted 100%.

Replies are listed 'Best First'.
Re^2: Can I see "requires"?
by JavaFan (Canon) on Mar 08, 2012 at 16:21 UTC
    A similar trick can be used to make sure code that uses use warnings works under pre-5.6 as well:
    BEGIN { if ($] < 5.006) { $INC{'warnings.pm'} = __FILE__; no strict 'refs'; *{'warnings::unimport'} = sub {0}; } }
    Of course, no warnings will be enabled when run under pre-5.6, but the code will not croak on a use warnings;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found