----- Forwarded message from Brad Lhotsky <[EMAIL PROTECTED]> -----

From: Brad Lhotsky <[EMAIL PROTECTED]>
To: Rodent of Unusual Size <[EMAIL PROTECTED]>
Subject: Re: defining 'constants' at run time
Date: Tue, 2 Mar 2004 14:01:06 -0500
User-Agent: Mutt/1.4i
Message-ID: <[EMAIL PROTECTED]>

Any reason why it has to be a constant?  I'm pretty sure you could
either find or write a Tie:: class that would only allow you to set the
value of a variable, or hash key once.  That might be a more
maintainable solution.

The code for this wouldn't be difficult if it doesn't already exist:

my $var = undef;
tie $var, 'Tie::Constant';

$var = 250;

$var = 1; # warning attempt to modify read-only value!

print "$var\n";  # '250\n';

?

On Tue, Mar 02, 2004 at 05:42:07AM -0500, Rodent of Unusual Size wrote:
> tell me to piss off for asking here, if you like, but i'm not on
> any other perl lists due to traffic.  besides, the responses and
> advice here have been higher quality. :-)
> 
> 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);
> 
> the result i'm looking for is 'DOH=25' on stdout.  however,
> all i've managed to get is either an error about DOH being a
> non-integer, or an unopened filehandle.  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?
> 
> (i hope i'm explaining this lucidly.)
> -- 
> #ken  P-)}
> 
> Ken Coar, Sanagendamgagwedweinini  http://Ken.Coar.Org/
> Author, developer, opinionist      http://Apache-Server.Com/
> 
> "Millennium hand and shrimp!"
> 
> 

-- 
Brad Lhotsky <[EMAIL PROTECTED]>

----- End forwarded message -----

-- 
Brad Lhotsky <[EMAIL PROTECTED]>

Reply via email to