Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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 having an uproarious good time at the Monastery: (13)
As of 2014-12-19 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (77 votes), past polls