Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by merlyn (Sage)
on May 22, 2002 at 20:52 UTC ( #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? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-05-17 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?