defining 'constants' at run time

2004-03-02 Thread Rodent of Unusual Size
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.)
-- 
#kenP-)}

Ken Coar, Sanagendamgagwedweinini  http://Ken.Coar.Org/
Author, developer, opinionist  http://Apache-Server.Com/

Millennium hand and shrimp!




Re: defining 'constants' at run time

2004-03-02 Thread Frederic Briere
On Tue, Mar 02, 2004 at 05:42:07AM -0500, Rodent of Unusual Size wrote:
 non-integer, or an unopened filehandle.  i've tried things
 like having mkconst() do
 
 my $name = caller() . ::$_[0];
 eval(\*$name = sub () { $_[1]; });

Look at constant.pm for a cleaner way to do this, using a closure.

 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?

Sure, just pre-declare the sub, which happens at compile time:

  sub DOH();

Then you can define it at run time, however you like.  Here's how to use
constant.pm for what it's good at:

  use constant;  # require would also work
  import constant DOH = 25;


-- 
 Frederic Briere*[EMAIL PROTECTED]

 =  [EMAIL PROTECTED] IS NO MORE:  http://www.abacomsucks.com  =


Re: defining 'constants' at run time

2004-03-02 Thread Simon Cozens
[EMAIL PROTECTED] (Rodent Of Unusual Size) writes:
 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?

No.

-- 
There seems no plan because it is all plan.
-- C.S. Lewis


Re: AW: defining 'constants' at run time

2004-03-02 Thread Simon Cozens
[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?

-- 
`And when you've been *plonk*ed by Simon C., you've been *plonked*
by someone who knows when, and why, and how.' - Mike Andrews, asr


Re: defining 'constants' at run time

2004-03-02 Thread Brad Lhotsky
- 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]


Re: defining 'constants' at run time

2004-03-02 Thread Brad Lhotsky
I forgot to CC the group.. but now I realized, when I originally read
his post I thought he was looking to define a variable once and then
leave it read only for the rest of the program execution which is why I
suggested the following.  However, I realize now that he's just trying
to avoid prefixing the symbols with ''.

This is a design issue more than anything.  As has been stated,
constants defined at run time aren't really constant.  Why do you need
to do this anyways?

On Tue, Mar 02, 2004 at 08:20:29PM -0500, Brad Lhotsky wrote:
 - 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.)
  -- 
  #kenP-)}
  
  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]

-- 
Brad Lhotsky [EMAIL PROTECTED]


Re: defining 'constants' at run time

2004-03-02 Thread A. Pagaltzis
* Jochen Stenzel [EMAIL PROTECTED] [2004-03-02 12:19]:
 the base technique is to build a use constant statement at
 runtime and evaluate it via eval().

No, if you are using constant.pm, the technique is to

require constant;
constant-import(FOO = 'bar');

eval(EXPR) is usually a red flag.

-- 
Regards,
Aristotle
 
If you can't laugh at yourself, you don't take life seriously enough.


Re: IO::Epoll

2004-03-02 Thread A. Pagaltzis
* Bruce J Keeler [EMAIL PROTECTED] [2004-03-02 09:57]:
 IO::Epoll exposes the low-level epoll system calls
 (epoll_create, epoll_ctl and epoll_wait), but it also has a
 higher-level OO API designed to be a drop-in replacement for
 IO::Poll.

As a stopgap solution f.ex to test the stability of your offering
that's ok. But since it can be written to be a drop-in
replacement it'd be preferrable for apps not have to include
system specific code; rather, IO::Poll should be able to use the
epoll(4) interface where available so all apps automatically
benefit. I'm sure Graham will gladly take a patch. :)

-- 
Regards,
Aristotle
 
If you can't laugh at yourself, you don't take life seriously enough.


Re: advice needed on Lingua::Identification

2004-03-02 Thread A. Pagaltzis
* [EMAIL PROTECTED] [EMAIL PROTECTED] [2004-02-26 17:29]:
 I'm putting together some things I have and creating a module
 named Lingua::Identification.

Just a comment on the name: personally, I'd much prefer
::Identify. It's half as long, says exactly the same thing, and
it feels less awkward to have the second word be a verb operating
on the first.

-- 
Regards,
Aristotle
 
If you can't laugh at yourself, you don't take life seriously enough.


Re: defining 'constants' at run time

2004-03-02 Thread A. Pagaltzis
* 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.


RE: defining 'constants' at run time

2004-03-02 Thread Pearce, Martyn
Are you sure that you want to do this?
As you're working at runtime, will you gain anything in defining a constant
rather than simply a global, or better, package-scope variable?

Mx.

-Original Message-
From: Rodent of Unusual Size [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, March 02, 2004 10:42 AM
To: [EMAIL PROTECTED]
Subject: defining 'constants' at run time


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!




Re: IO::Epoll

2004-03-02 Thread Rocco Caputo
On Mon, Mar 01, 2004 at 08:08:01PM -0800, Bruce J Keeler wrote:
 The epoll subsystem is a new, (currently) Linux-specific variant of
 poll(2).  It is designed to offer O(1) scalability over large numbers of
 watched file descriptors.  You need at least Linux 2.5.44 and a recent C
 library to use it.

Have you considered a more portable solution like libevent?  It's a
single API for epoll (Linux), kqueue (BSD), poll(2), and select(2).
Support for realtime signals and /dev/poll is also planned.

Its home page is at http://monkey.org/~provos/libevent/

-- 
Rocco Caputo - [EMAIL PROTECTED] - http://poe.perl.org/