Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: using constants in Derived class

by moritz (Cardinal)
on Nov 17, 2010 at 10:36 UTC ( [id://871975]=note: print w/replies, xml ) Need Help??


in reply to using constants in Derived class

I'd like to bring some curious implementation details to your attention:
  1. use constant works by automatically generating nullary subroutine that returns a constant. The optimizer then optimizes away the call, and replaces it by the constant itself.
  2. All subroutines can be used as method
  3. In method calls, the arity check from the prototype that  use contant ... installed isn't performed

Combining these curious features, you'll notice that

  • If there was use constant a => 1 in a class, you can actually access that value with $obj->a
  • If a child class has another use constant a => 2;, the $obj->a syntax returns the value from the derived class

So if you access the constant as methods, everything should work according to your wishes. What a coincidence :-)

That way it's much more extensible than using a big if-elsif-else construct, and I consider that a good thing. It's a typical OO style, and if your application is already OO, it's good to conform to that stlye.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found