in reply to Re: OOP - Constant Vs. Subroutine
in thread OOP - Constant Vs. Subroutine

subclasses can override constants too.

Replies are listed 'Best First'.
Re^3: OOP - Constant Vs. Subroutine
by perrin (Chancellor) on May 11, 2007 at 21:57 UTC
    How would you do that? Would you have to call it like a method?
    Class->constant;
    That would prevent the in-lining behavior, but I guess it's no worse than a normally defined simple method.

      Not really, Class->constant won't inherit from Superclass->constant ; it needs to be defined in both.

      however $classObject->constant will inherit constants from superclass as class object methods , and allow the ability to override

      so...

      package aaa; use constant HELLO=> 'world'; use constant PUNCTUATION=> '!'; sub new { #std constructor here } package bbb; our @ISA= qw( aaa ); use constant HELLO=> 'universe'; package main; my $a= aaa->new(); my $b= bbb->new(); print "\n Hello " . $a->HELLO . $a->PUNCTUATION ; print "\n Hello " . $b->HELLO . $b->PUNCTUATION ;

      will get you

      Hello world! Hello universe!

      but bbb->PUNCTUATION doesn't exist

        but bbb->PUNCT­UATION doesn't exist

        Did you try it?

        package aaa; use constant HELLO=> 'world'; use constant PUNCTUATION=> '!'; package bbb; our @ISA= qw( aaa ); use constant HELLO=> 'universe'; package main; print "\n Hello ", aaa->HELLO(), aaa->PUNCTUATION(); print "\n Hello ", bbb->HELLO(), bbb->PUNCTUATION();

        produces:

        Hello world! Hello universe!

        - tye