develooper Front page | perl.perl5.porters | Postings from August 2001

Re: The coderef in @INC strikes back

Thread Previous | Thread Next
From:
Philippe M . Chiasson
Date:
August 22, 2001 13:16
Subject:
Re: The coderef in @INC strikes back
Message ID:
20010822162030.E1948@eXtropia.com
On Wed, Aug 22, 2001 at 08:27:10AM +0100, Jonathan Stowe wrote:
> On Tue, 21 Aug 2001, Rafael Garcia-Suarez wrote:
> 
> > Do you remember this little odd feature that allows to add
> > a coderef in @INC? See the digest for info :
> >
> > http://www.perl.com/pub/a/2001/03/p5pdigest/THISWEEK-20010305.html#Coderef_INC
> >
> > AFAIK, this feature has been left unfinished in the core :
> > there are no docs and no tests for it.
> >
> 
> So I sat down with my computer with a view to seeing what I could do about
> this and whilst writing a test to determine what use could be made of the
> coderef that is passed to the subroutine I  came up with :
> 
> #!/usr/bin/perl -w
> 
> use strict;
> 
> BEGIN
> {
>    push @INC , bless( sub { print "*@_*" }, 'Foo');
> }
> 
> use Foggle;
> 
> 
> Which to my surprise gives :
> 
> Can't locate object method "INC" via package "Foo" (perhaps you forgot to
> load "Foo"?) at inctest.pl line 10.
> BEGIN failed--compilation aborted at inctest.pl line 10.

You didn't place a CODE ref in @INC, but a blessed object.

In the case of a CODE ref, like this :

#!/usr/bin/perl -w
 
use strict;
 
BEGIN
{
   push @INC ,sub { 
   		print "*@_*";
		open (FH, "/dev/null");
		return \*FH;
		};
}
use Foogle;	

you will see it works :

Foggle.pm did not return a true value at t.pl line 14.

If you put a blessed thing in there, it attempts to call ->INC('Foogle.pm') on it
instead.

#!/usr/bin/perl -w
 
use strict;
 
BEGIN
{
	sub foo::INC {
		print "*@_*";
		open (FH, "/dev/null");
		return \*FH;
		}
	push @INC , bless {} ,'foo';
}
 
use Foggle;

But for this to work, you need a _fairly_ recent version of perl.

Hope this helps

> 
> Now maybe I shouldnt be surprised and maybe I have missed something here
> but to me this looks like it wants me to define Foo::INC() - but even
> doing that doesnt help.
> 
> A cursory examination of the code of pp_require() seems to suggest that it
> should work - is this just not finished ?
> 
> /J\
> -- 
> Jonathan Stowe                      |
> <http://www.gellyfish.com>          |      This space for rent
>                                     |
> 

-- 
Philippe M. Chiasson  <gozer@cpan.org>
  Extropia's Resident System Guru
     http://www.eXtropia.com/

Intelligent, well meaning programmers can insert bugs more
quickly than they can be removed.
	-- Greg Stein

perl -e '$$=\${gozer};{$_=unpack(P26,pack(L,$$));/^Just Another Perl Hacker!\n$/&&print||$$++&&redo}'

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About