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 (package aliasing)

by LanX (Sage)
on Apr 11, 2019 at 17:20 UTC ( #1232463=note: print w/replies, xml ) Need Help??


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

This is pretty clean, put your private stuff into a sub-package and alias that long package path it to a shorter package name at compile time.

(of course these subs won't be able to call imported subs in the parent package without full qualification or inheritance ;-)

use strict; use warnings; package X::Y::Z::my; # sub package sub xyz {warn "xyz called" } package Y::Y::Z; BEGIN { *::my:: = *X::Y::Z::my:: ; # alias sub package to my:: } # END { # delete alias at file's end # delete $::{"my::"}; # } my::xyz(); BEGIN { # delete alias here for demo delete $::{"my::"}; } package Other; my::xyz(); # fails alias already gone

C:/Perl_524/bin\perl.exe d:/exp/local_package.pl xyz called at d:/exp/local_package.pl line 6. Undefined subroutine &my::xyz called at d:/exp/local_package.pl line 3 +0.

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

update

the magic in the BEGIN/END blocks could be done in the importer of a module called with

use  PackageAlias "X::Y:Z::my" => "my" and no PackageAlias "my"

Update

I just realised that Package::Alias already exists, even with a pretty similar interface.

Skipped colons in my example to avoid confusion.

  • Comment on Re: Making it clearer to say that a sub is defined within current package (package aliasing)
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2022-05-25 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?