Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Optimized constants without namespace pollution? constant::co?

by Anonymous Monk
on Feb 16, 2013 at 09:31 UTC ( #1019025=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Optimized constants without namespace pollution? constant::co?

This doesn't work

#!/usr/bin/perl -- package NameSpace; use constant 1 +{qw{ RO 1 SHAM 2 BO 3 }}; BEGIN { print "before clean ", join ' ', keys \%NameSpace::, "\n"; } use namespace::clean; use namespace::clean qw[ RO SHAM BO ]; BEGIN { print "after clean ", join ' ', keys \%NameSpace::, "\n"; } printf "SHAM?? %s \n", SHAM(); 1; __END__ $ perl -e " do 'yo'; print NameSpace::SHAM() " before clean BEGIN RO BO SHAM after clean BEGIN __NAMESPACE_CLEAN_STORAGE RO BO SHAM SHAM?? 2 2

I think it should be possible to accomplish using Devel::Declare or maybe another way, but I don't know how

update: I found PerlX::QuoteOperator but it doesn't constant-ize (not optimized):

#!/usr/bin/perl -- package NameSpace; { use PerlX::QuoteOperator CO => { -emulate => 'q', -with => do { my %CO = qw[ RO 1 SHAM 2 BO 3 ]; sub ($) { $CO{ uc $_[0]} }; }, }; print CO(RO), "\n"; #~ print CO'SHAM', "\n"; ## Can't find string terminator "'" anywh +ere before EOF print CO"SHAM", "\n"; print CO/BO/, "\n"; } print "OUTSIDE", CO(SHAM), "\n"; eval q{ print "from eval ", CO(SHAM), "\n"; } or die $@; __END__ 1 2 3 OUTSIDE2 from eval 2

It CO"SHAM" becomes/Deparses as  CO('SHAM')

Can Devel::Declare make constants, ie CO(SHAM) becomes "SHAM" instead of CO("SHAM") ?

Replies are listed 'Best First'.
Re: Optimized constants without namespace pollution? constant::co?
by tobyink (Abbot) on Feb 16, 2013 at 11:40 UTC

    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019025]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2016-06-26 14:36 GMT
Find Nodes?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...

    Results (330 votes). Check out past polls.