Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Cancel/no-op a require

by tobyink (Abbot)
on Dec 07, 2012 at 13:16 UTC ( #1007733=note: print w/ replies, xml ) Need Help??


in reply to Cancel/no-op a require

Try thanks.

no thanks qw(Foo Bar); use Foo; # no-op require Bar; # no-op
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Cancel/no-op a require
Download Code
Re^2: Cancel/no-op a require
by sedusedan (Monk) on Dec 07, 2012 at 13:36 UTC

    :) (obvious pun avoided)

    My problem at hand is actually grok-ing the @INC import hook API and avoiding duplicate importing. I'm trying to add a feature to Log::Any::For::Package to install import hook. If user specifies Foo::* in the list of packages to add logging to, then after some Foo::* module is loaded, I want the hook to add logging to the newly loaded package automatically. With this:

    unshift @INC, sub { my ($self, $name) = @_; # load the module first local @INC = grep { !ref($_) || $_ != $self } @INC; require $name; # add logging to the package # instruct Perl to not load the module again };

    Perl is loading the module twice, resulting in "subroutine X redefined" errors.

    This works, BTW, though I'm not sure it is proper (or even, exactly how).

    unshift @INC, sub { my ($self, $name) = @_; $INC{$name} = 1; # load the module first local @INC = grep { !ref($_) || $_ != $self } @INC; require $name; # add logging to the package return (); };

      OK, so now the correct incantation should be:

      unshift @INC, sub { my ($self, $name) = @_; # load the module first local @INC = grep { !ref($_) || $_ != $self } @INC; require $name; # add logging to the package # ignore this hook my $line = 0; return sub { unless ($line++) { $_ = "1;\n"; return 1; } return 0; } };

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2015-07-06 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls