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") ?
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |