Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using constant to override (abstract) methods.

by ELISHEVA (Prior)
on Mar 28, 2011 at 09:39 UTC ( [id://895873]=note: print w/replies, xml ) Need Help??


in reply to Using constant to override (abstract) methods.

The more important issue to me is why?

Although your approach 'works', I should think the use of constant to define overrides would be very confusing to future maintainers. For one, not everyone knows that constants are actually subs. Your coworker's confusion and many posts here I think testify to that. (look on Super Search).

Second, use constant is not the obvious place to look for a method that overrides a superclass method.

Third, it is somewhat misleading. The word "constant" implies immutable. That might lead someone to think that you have defined a final method. Perhaps that is even your reason for wanting to use "constant" this way? However, methods defined via use constant are not final. They can be overridden by any subclass because they are, after all subs. For example,

use strict; use warnings; package Foo; use constant X => 100; sub new { bless([], $_[0]); } package Goo; our @ISA=qw(Foo); use constant X => 200; sub new { bless([], $_[0]); } package MeToo; our @ISA=qw(Foo); sub X { print "Hello World\n"; } my $oFoo = Foo->new(); my $oGoo = Goo->new(); my $oMeToo = MeToo->new(); print "$oFoo -> X = ", $oFoo->X, "\n"; print "$oGoo -> X = ", $oGoo->X, "\n"; $oMeToo->X(); #outputs Foo=ARRAY(0x8218ed0) -> X = 100 Goo=ARRAY(0x8218f10) -> X = 200 Hello World

Performance wouldn't be a reason to use constants in this way either. Perl optimizes constant methods even when they are not explicitly defined as constants. According to Constant Functions:

Functions with a prototype of () are potential candidates for inlining. If the result after optimization and constant folding is either a constant or a lexically-scoped scalar which has no other references, then it will be used in place of function calls made without & .

There is nothing special about use constant. You can see the source code for the use constant pragma here. It is just Perl code that converts use constant X => Y into  sub X() { Y }. If anything, you are adding overhead to your startup code as compared with a simple sub definition since the pragma has to analyze each X => Y and generate the necessary symbols.

Update: added comments about performance.

Replies are listed 'Best First'.
Re^2: Using constant to override (abstract) methods.
by klekker (Pilgrim) on Mar 28, 2011 at 10:27 UTC
    So you are asking 'Why?!'? That's why I like reading PM! :)

    The example lacks some documentation that is included in the 'real' code, so I'm confident that my colleagues can deal with the code.

    But more important and interessting: 'why'?

    First of all: I don't claim that this is the best or most clever solution.

    The motivation for this was, that we have a bunch of small classes with immutable values (like for example: error-classes).
    You've got FileError, DatabaseError, FooError... all these errors share an errorCode, an errorText, an errorDescription and also a static list of errorFields which the error could affect.
    So I decided to create an interface Error for these errors so that every error-class (which are using constants so far) really implements all constants.

    k
Re^2: Using constant to override (abstract) methods.
by klekker (Pilgrim) on Mar 31, 2011 at 11:54 UTC
    Dear ELISHEVA,

    I reconsidered your comment and talked about it with some colleagues.
    Now I have to confess that I changed the implementation and replaced constant by subroutines and let the code speak for itself.

    Thank you again for your feedback! :)

    k

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found