Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

•Re: constant and eval (are they enemies?)

by merlyn (Sage)
on May 22, 2002 at 20:52 UTC ( [id://168580]=note: print w/replies, xml ) Need Help??


in reply to constant and eval (are they enemies?)

Let's say you wanted to make CONSTANT1 be 1, CONSTANT2 be 2, and so on. This loop does the same as constant:
BEGIN { no strict 'refs'; for (1..5) { my $num = $_; *{__PACKAGE__ . "::CONSTANT$num"} = sub () { $num }; } }
It's important that $num here be a closure variable, so that all five coderefs are distinct.

You should be able to extrapolate this strategy to your application.

-- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://168580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found