Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Use of %INC ?

by vinoth.ree (Monsignor)
on Jul 18, 2009 at 09:36 UTC ( #781287=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I read that %INC is a special Perl variable that is used to cache the names of the files and the modules that were successfully loaded and compiled by use(), require() or do() statements. Before attempting to load a file or a module with use() or require(), Perl checks whether it's already in the %INC hash. If it's there, then the loading and therefore the compilation are not performed at all. Otherwise, the file is loaded into memory and an attempt is made to compile it.

Where can I realize the use of variable ?

update

Title and question has been changed

update

bold tag modified

Replies are listed 'Best First'.
Re: Use of %INC ?
by FunkyMonk (Chancellor) on Jul 18, 2009 at 11:02 UTC
    You use %INC just like any other hash, except that its populated automatically by do, require and use. See perlvar for more details.

    use strict; print "$_: $INC{$_}\n" for keys %INC; __END__ strict.pm: /usr/share/perl/5.10/strict.pm

    Writing to %INC does have its uses. For example, when you want to define classes inside a main script:

    use strict; use warnings; BEGIN { package Foo; sub new { bless {}, $_[0] } sub ima { "I'm a '", ref $_[0], "' object\n" } $INC{'Foo.pm'} = "Anything at all. It really doesn't matter"; } use Foo; my $F = Foo->new; print $F->ima, "\n"; __END__ I'm a 'Foo' object

    This code dies (Can't locate Foo.pm in @INC) without the assignment to %INC. merlyn uses this technique often in his series of articles about Rose::DB.

Re: Use of %INC ?
by JavaFan (Canon) on Jul 18, 2009 at 10:23 UTC
    I don't know what your question is. Do you mean "how can I use this variable?" In that case, the answer is, "in the same way you use any other hash". Or perhaps you mean "when should I use this variable?" In which case the answer is "probably never, and if you do, just for read access".

      When should I use this variable? this is what my questions.

        As javafan said, "probably never, and if you do, just for read access".

        Perl use this variable to remember which modules have already been loaded, and kindly allows you to see it. This doesn't meen that you are compelled to fiddle with it. The only use I can think of at present, is to see if a module has been loaded, perhaps by other modules.

        Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

        Post JavaFan correction: Ignore this please for it is disinformation. Or, run s[\%INC][\@INC]g before reading.

        Use it when you are trying to see just what kind of gunk^Wdirectory paths %INC is filled up with, when you know you have installed a module but perl is failing to find one.

        Other similar use is when trying to sort out the module paths, say to clean up the duplicates.

        Well, that is when I personally (have) use(d) %INC anyway.

        I've typically used it for either finding out what modules are loaded, or for trying to track down where they're loaded from (when you're cleaning up an old machine, and can't figure out why you're not getting the version of the module you're expecting.)

        I keep the following one-liner shell script around as 'whichpm' :

        echo 'print map { sprintf( "%20s : %s\n", $_, $INC{$_} ) } sort keys %INC; print "\n'$1' version : $'$1'::VERSION\n\n"' | perl "-M$1"
Re: Use of %INC ?
by ig (Vicar) on Jul 18, 2009 at 15:47 UTC

    There are usually quite a few directories listed in the @INC array and it is possible for a module to be installed in more than one of these directories. This might happen, for example, if you install a core module (one of the modules distributed with the perl program) from CPAN. Your installation from CPAN will usually create a new copy of the module in a different directory rather than overwriting the copy provided with perl. Your perl program will usually find the newer version, but this will depend on where it was installed and how @INC is set at the time the module is loaded.

    By printing $INC{'Module.pm'}, you can find out which copy of the module was loaded.

    By printing all of %INC (e.g. using Data::Dumper) you can find out what all of the loaded modules are.

Re: Use of %INC ?
by mzedeler (Pilgrim) on Jul 18, 2009 at 13:35 UTC

    Try it out yourself. Create a script like so:

    print "Hello\n";

    Put it in a file named hello.pl and run this (untested):

    print "1: ", join ', ', %INC, "\n"; do 'hello.pl'; print "2: ", join ', ', %INC, "\n"; use Data::Dumper; print "3: ", join ', ', %INC, "\n";
Re: Use of %INC ?
by Anonymous Monk on Jul 18, 2009 at 09:54 UTC
    Where can I realize the use of variable ?

    What?

      Sorry, I am asking the usage of the content in the %INC variable

Re: Use of %INC ?
by Bloodnok (Vicar) on Jul 18, 2009 at 14:35 UTC
    Not quite ... you say ...If it's there, then the loading and therefore the compilation are not performed at all... - this is only true of require, use and indeed do, _always_ load the file.

    The use of the hash [%INC] in testing is nicely demonstrated in perl Testing - A developers notebook - to name but one.

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2022-11-30 21:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?