Simon Cozens wrote:

[EMAIL PROTECTED] (Jochen Stenzel) writes:


the base technique is to build a "use constant" statement at runtime and evaluate it via eval().



% perl -w -Mstrict -le 'eval "use constant FOO => 3"; print FOO' Name "main::FOO" used only once: possible typo at -e line 1.
print() on unopened filehandle FOO at -e line 1.
%


Any other ideas?


you obviously can't compile the code you want the constants to be in until after you
have defined them -- otherwise they're not constants -- so define the constants before
you compile the code that sees them


perl -w -Mstrict -le 'BEGIN{eval "use constant FOO => 3"}; print FOO'
3

and if you don't really want a constant but rather an undecorated scalar that
won't interpolate, there is always the empty prototype. I like to think of
C<use constant> as a shorthand for creating constant subroutines so I
generally just set up the constant subroutines when I need a named constant


perl -w -Mstrict -le 'BEGIN{eval "sub FOO(){3}"}; print FOO'
3


I wonder what aspect of "constant" is desired to be kept. If it is lack of decoration,

Furthermore,
perl -w -Mstrict -le 'sub FOO(){3}; print FOO; *FOO = sub(){4}; print FOO'
3
Constant subroutine main::FOO redefined at -e line 1.
3

but folding can be suppressed
perl -w -Mstrict -le 'sub FOO(){my$x; 3}; print FOO; *FOO = sub(){4}; print FOO'
3
Subroutine main::FOO redefined at -e line 1.
4





-- [EMAIL PROTECTED] Include phrase "cat and buttered toast" to get through my filter



Reply via email to