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") ?


In reply to Optimized constants without namespace pollution? constant::co? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.