diff options
author | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2001-08-25 01:34:13 +0200 |
---|---|---|
committer | Nick Ing-Simmons <nik@tiuk.ti.com> | 2001-08-25 14:32:47 +0000 |
commit | d54b56d55c935aeda35fad4725273ab2eb5a71a5 (patch) | |
tree | d95e6415b26a3d53c83c337213d592a53b4f1a77 | |
parent | e14e2dc8035bb82ccaf583f5f04cf14f72dcd147 (diff) | |
download | perl-d54b56d55c935aeda35fad4725273ab2eb5a71a5.tar.gz |
[DOC PATCH] The coderef-in-@INC feature
Message-Id: <20010824233413.A1285@rafael>
p4raw-id: //depot/perl@11743
-rw-r--r-- | pod/perlfunc.pod | 51 | ||||
-rw-r--r-- | pod/perlvar.pod | 4 |
2 files changed, 55 insertions, 0 deletions
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 56937f4ddf..b4c37112c7 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -3808,6 +3808,57 @@ will complain about not finding "F<Foo::Bar>" there. In this case you can do: eval "require $class"; +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. "F<Foo/Bar.pm>"). The +subroutine should return C<undef> or a filehandle, from which the file to +include will be read. If C<undef> is returned, C<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 + ... + } + +or: + + 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 +C<main>.) Here is a typical code layout: + + # In Foo.pm + package Foo; + sub new { ... } + sub Foo::INC { + my ($self, $filename) = @_; + ... + } + + # In the main program + push @INC, new Foo(...); + For a yet-more-powerful import facility, see L</use> and L<perlmod>. =item reset EXPR diff --git a/pod/perlvar.pod b/pod/perlvar.pod index 3f4c2f5255..cdbd53ffe8 100644 --- a/pod/perlvar.pod +++ b/pod/perlvar.pod @@ -1062,6 +1062,10 @@ loaded also: use lib '/mypath/libdir/'; use SomeMod; +You can also insert hooks into the file inclusion system by putting Perl +code directly into @INC. Those hooks may be subroutine references, array +references or blessed objects. See L<perlfunc/require> for details. + =item @_ Within a subroutine the array @_ contains the parameters passed to that |