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

can has ELI5 namespace::autoclean pls?

by Ea (Chaplain)
on Feb 15, 2019 at 14:58 UTC ( #1229960=perlquestion: print w/replies, xml ) Need Help??

Ea has asked for the wisdom of the Perl Monks concerning the following question:

Friday afternoon question: what's the real purpose of namespace::autoclean in your OO code?

I've seen it in classes written with Moose, but the description Keep imports out of your namespace doesn't really speak to me about the advantage of using it. Where am I going to get bitten if I don't use it. Just a simple answer will do.

thanks,

Ea

Sometimes I can think of 6 impossible LDAP attributes before breakfast.

Mojoconf was great!

Replies are listed 'Best First'.
Re: can has ELI5 namespace::autoclean pls?
by Haarg (Curate) on Feb 15, 2019 at 15:33 UTC

    If you don't clean your imports using something like namespace::autoclean or namespace::clean, then they will remain and be callable as methods on your objects. So something like the following will work and issue a warning with your object stringified:

    package Foo; use Moose; use Carp; Foo->new->carp;

    This is usually not harmful, although it can be unexpected or allow people to accidentally call things in unusual ways. Cleaning your imports also allows you to re-use those imported sub names as method, such as for attributes. That means that the following will work:

    package Foo; use Moose; use namespace::autoclean; has after => (is => 'ro', default => 'yay'); print "It worked " . Foo->new->after;

    Normally, after would be one of Moose's helpers that creates a method modifier, but cleaning it using namespace::autoclean allows an attribute with the same name to be created.

      So other than providing a clean API -- for your first point, it could also help to prevent those hard to spot bugs when you inherit (or use a role) with the same name as that utility function you imported as a one-off. I'm thinking of action at a distance.

      I'm also not great at remembering which keywords are reserved in which problem domains.

      oohhh

      Just tried your code and the second example is insidious. I can see the error thrown when I remove namespace::autoclean from your example, but when I do it in a module and run the test it passes, no error. For me, intermittent errors are the worst. I think ns::ac just became part of my boilerplate.

      Ea

      Sometimes I can think of 6 impossible LDAP attributes before breakfast.

      Mojoconf was great!

Re: can has ELI5 namespace::autoclean pls?
by choroba (Archbishop) on Feb 15, 2019 at 17:02 UTC
    But be careful, sometimes namespace::autoclean doesn't work as one would think.

    tl;dr: namespace::autoclean relies on Class::MOP::Class::get_method_list to get the list of methods and removes everything else from the namespace. Unfortunately, the subroutine lists e.g. constants imported from the POSIX module. So, using namespace::clean, which is more explicit, might be cleaner (no pun intended).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1229960]
Front-paged by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-12-02 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (32 votes). Check out past polls.

    Notices?