Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: A simple import() for those special moments

by MarkM (Curate)
on Jan 19, 2003 at 06:14 UTC ( #228134=note: print w/replies, xml ) Need Help??


in reply to A simple import() for those special moments

For some modules, I have come to the conlusion that nothing more than the following is necessary:

sub import { my $caller = caller; *{$caller . '::FUNCTION1'} = \&FUNCTION1; *{$caller . '::SCALAR1'} = \$SCALAR1; }

I usually end up with code such as the above for very specialized modules that need to load quickly, without any dependencies. Exporter.pm, even split into Exporter.pm and Exporter/Heavy.pm, is beastly long for the most common usages.

Some benefits of this approach include the ability to rename objects, and the ability to export lexical objects (in the above case, $SCALAR1 could be declared with my()).

I am not recommending my solution over yours. I am agreeing that rolling your own import() function can be very practical, and I believe that the practice suits the Perl mindset. Exporter.pm is one (convenient?) way to do it. It isn't the only way to do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://228134]
help
Chatterbox?
shmem serves tea along with the cookies. (☕) x @CBMonks
[shmem]: hmm.. which makes emerge the question: is there a way to attach a subroutine to a variable, which is run as soon as that variable changes?
[moritz]: through a tie?
[shmem]: moritz: of course!
shmem slaps forehead m(
[shmem]: consider, then, @CBMonks as a tied variable which triggers withdrawing empty cups and serving new ones

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2017-04-24 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (439 votes). Check out past polls.