in reply to can has ELI5 namespace::autoclean pls?

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.

Replies are listed 'Best First'.
Re^2: can has ELI5 namespace::autoclean pls?
by Ea (Chaplain) on Feb 15, 2019 at 16:24 UTC
    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.


    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.


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

    Mojoconf was great!