Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 lurking in the Monastery: (6)
As of 2014-07-14 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (254 votes), past polls