http://www.perlmonks.org?node_id=1019033


in reply to Optimized constants without namespace pollution? constant::co?

You're overthinking this. constant and namespace::clean do play nicely together...

use 5.010; use strict; use warnings; use Test::More; { package Foo; use constant XYZ => 123; use namespace::clean; sub add_xyz { shift; return XYZ + $_[0] }; } ok( !Foo->can('XYZ'), 'constant was cleaned away' ); ok( Foo->can('add_xyz'), 'method still exists' ); is( Foo->add_xyz(100), 223, 'method works properly' ); done_testing;

The problem with your tests is that you assume that use namespace::clean deletes subs immediately. It does not. It uses B::Hooks::EndOfScope to wipe the subs away when the enclosing scope is finished compiling.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name