Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Subroutine reference in @INC

by gellyfish (Monsignor)
on Mar 21, 2002 at 21:52 UTC ( #153436=note: print w/replies, xml ) Need Help??

in reply to Subroutine reference in @INC

Yep, you can do that but I think you are going to be looking at the forthcoming 5.8.0 release to be able to do that in the best fashion ...I am sorry but I can't find the stuff on this right now.


Replies are listed 'Best First'.
Re: Re: Subroutine reference in @INC
by IlyaM (Parson) on Mar 21, 2002 at 22:23 UTC
    Thanks for hint about 5.8.0. I've just checked docs for 5.7.3 and I found description of this feature. It seems that interface is not going to be changed in 5.8.0. It just becomes documented.

    From perldoc -f require in 5.7.3:

    You can also insert hooks into the import facility, by putting directly Perl code into the @INC array. There are three forms of hooks: subroutine references, array references and blessed objects.

    Subroutine references are the simplest case. When the inclusion system walks through @INC and encounters a subroutine, this subroutine gets called with two parameters, the first being a reference to itself, and the second the name of the file to be included (e.g. "Foo/"). The subroutine should return "undef" or a filehandle, from which the file to include will be read. If "undef" is returned, "require" will look at the remaining elements of @INC.

    If the hook is an array reference, its first element must be a subroutine reference. This subroutine is called as above, but the first parameter is the array reference. This enables to pass indirectly some arguments to the subroutine. In other words, you can write:

    push @INC, \&my_sub; sub my_sub { my ($coderef, $filename) = @_; # $coderef is \&my_sub ... }


    push @INC, [ \&my_sub, $x, $y, ... ]; sub my_sub { my ($arrayref, $filename) = @_; # Retrieve $x, $y, ... my @parameters = @$arrayref[1..$#$arrayref]; ... }

    If the hook is an object, it must provide an INC method, that will be called as above, the first parameter being the object itself. (Note that you must fully qualify the sub's name, as it is always forced into package "main".) Here is a typical code layout:

    # In package Foo; sub new { ... } sub Foo::INC { my ($self, $filename) = @_; ... } # In the main program push @INC, new Foo(...);

    Note that these hooks are also permitted to set the %INC entry corresponding to the files they have loaded. See "%INC" in perlvar.

    Ilya Martynov (

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://153436]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-07-19 16:00 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (411 votes). Check out past polls.