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

Re: Code Reuse

by LanX (Saint)
on Aug 20, 2017 at 17:58 UTC ( [id://1197710]=note: print w/replies, xml ) Need Help??


in reply to Code Reuse

A use is basically a require plus a call to the import sub of the included package.

And import is executed in the context of the calling package.

Thus you can call other imports inside a generic import to export into the calling namespace.

For instance:

package generic; use strict; # * ... sub import { strict->import(); ... }

You might wanna have a look into the source of Modern::Perl for more examples.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Update

*) Had to change require strict; to use strict; to make it work. (not sure why)

Answer

It's a timing issue, the require has to happen in a BEGIN or inside the import (use does it automatically), this works

package generic; sub import { require strict; strict->import(); ... } 1;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-24 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found