Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: using constants in Derived class

by moritz (Cardinal)
on Nov 17, 2010 at 10:36 UTC ( #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.


Comment on Re: using constants in Derived class
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-08-30 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls