Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

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


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

> My::Dumper() would both call Data::Dumper::Dumper()

It's definitely possible to tell in which package a sub was declared.

I saw it in the book perlhacks and IIRC it's available in B

The problem I see is that My:: is global, you would need the hint hash to restrict it's effect only to the file importing My.

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

  • Comment on Re^2: Making it clearer to say that a sub is defined within current package

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

    Well that's an issue if you want to set *My::Dumper = *Data::Dumper::Dumper once and for all. But if the only function defined in my is My::AUTOLOAD, and it always checks the calling package you should be fine wouldn't you? (At execution time at least, you'd lose compilation time errors with My::function() instead of function())

      In a clean design you don't want My:: to have effect outside the scope using it.

      Also probably a lowercase name my:: for pragma would be more appropriate to avoid a clash with cpan modules.

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

        Checking caller and dying when the function does not exist in the caller package wouldn't be much different than dying when calling an undefined function with the package prefix.

        The main problem is that calling local functions as my::something() makes strict's job harder.

Re^3: Making it clearer to say that a sub is defined within current package
by LanX (Saint) on Apr 11, 2019 at 16:43 UTC
    > It's definitely possible to tell in which package a sub was declared.

    From Sub::Info

    my $cobj = B::svref_2object($sub); my $name = $cobj->GV->NAME; my $file = $cobj->FILE; my $package = $cobj->GV->STASH->NAME;

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found