Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Optimized constants without namespace pollution? constant::co?

by tobyink (Abbot)
on Feb 16, 2013 at 11:40 UTC ( #1019033=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Optimized constants without namespace pollution? constant::co?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2015-07-28 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (259 votes), past polls