* Rodent of Unusual Size <[EMAIL PROTECTED]> [2004-03-02 11:43]: > i want to be able to define 'constants' in a module at run > time, rather than at compile time. 'use constant' does it too > early. for example: > > use Foo; > my $foo = new Foo; > $foo->mkconst('DOH', 25); > printf("DOH=%d\n", DOH);
Offtopic rant: why do people want/use OO for things like these??? Why not simply have a Foo::mkconst() ? > i've tried things like having mkconst() do > > my $name = caller() . "::$_[0]"; > eval("\*$name = sub () { $_[1]; }"); > > and that will work -- IFF the caller refers to DOH as &DOH. > > evidently the usage of DOH in the caller has already been > fetched from the symbol table (and found wanting) before the > method has been called. is there any way to defeat that? The problem is that perl doesn't know DOH is a function call unless it has seen a function with that name. So you have to declare the constant name, if not the constant value, at compile time. There's no way around this, period. In case of constants there's an extra complication: $ perl -MO=Deparse,-x7 -Mconstant=FOO,BAR -e'print FOO' use constant (split(/,/, 'FOO,BAR', 0)); print 'BAR'; -e syntax OK You'll notice that perl has substituted the value of FOO for the call, *at*compile*time*. You cannot do that with runtime defined constants for obvious reasons. -- Regards, Aristotle "If you can't laugh at yourself, you don't take life seriously enough."