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

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://871975]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-06-20 12:16 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.