Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Making it clearer to say that a sub is defined within current package

by LanX (Saint)
on Apr 11, 2019 at 15:23 UTC ( [id://1232445]=note: print w/replies, xml ) Need Help??


in reply to Making it clearer to say that a sub is defined within current package

Like I told you in the CB your "approach" doesn't even work.

Importing means aliasing into the current package, hence any function will be available.

Your approach is quite inside out, it's common to import explicitly, hence use module qw/one two three/ makes it obvious, even if module is badly designed and exports one two and three by default.

There is also the convention to mark private methods with a leading underscore, so _xyz() would be obvious for the reader.

In order to enforce privacy you can also use private code refs like my $xyz=sub {} and dereference every call. It's not (easily) possible to import private variables so this is safe. There is AFAIK also an experimental feature to allow private sub declarations.

If all of this is not enough ... Sigh.... you could hack yourself a solution, by aliasing the "local" subs to another temporary package like "_" and call _::xyz(). This export must happen at compile time and prior to any call.

IIRC it's possible to use introspection to determine where a sub was declared, hence you could even automate this aliasing in a module use _ where the importer inspects the caller's stash.

Brain fucked and you'll probably sabotage OOP inheritance but hey it's Perl and TIMTOWTDI... =)

Update

Just saw Eily's post and his "My::" seems to correspond to my "_::" . Using AUTOLOAD is another option but IMHO this would come with a performance penalty.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Making it clearer to say that a sub is defined within current package
by Eily (Monsignor) on Apr 11, 2019 at 15:29 UTC

    Hu, wish I had read what you said before starting posting. I realized the issue with imported functions *after* writing most of it so I added the issue as an afterthought. There's still the option of looking inside @EXPORT lists to only create the aliases for the correct functions though :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-24 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found