klekker has asked for the wisdom of the Perl Monks concerning the following question:
Dear fellow monks.
In a recent project I used an abstract base class as an interface and implemented its abstract methods using constants (for the sake of clarity; please see my code below).
Reading the docs for 'constants' I can't see anything wrong with this usage (constants become inline functions at compile time and I can override functions).
Now a colleague claims that I cannot rely on this behaviour because constants become *inline* functions and so the lookup might crash in some (unknown?) cases.
Well, the code works. But since it is supposed to run on a customers system and I don't want to have to fix it later if we run into trouble, I've decided to approach a higher instance:
so esteemed monks let me please ask you: is this approach okay or do you see any problems?
Or better: is this implementation considered harmful?
Thanks in advance!
k
Father.pm
In a recent project I used an abstract base class as an interface and implemented its abstract methods using constants (for the sake of clarity; please see my code below).
Reading the docs for 'constants' I can't see anything wrong with this usage (constants become inline functions at compile time and I can override functions).
Now a colleague claims that I cannot rely on this behaviour because constants become *inline* functions and so the lookup might crash in some (unknown?) cases.
Well, the code works. But since it is supposed to run on a customers system and I don't want to have to fix it later if we run into trouble, I've decided to approach a higher instance:
so esteemed monks let me please ask you: is this approach okay or do you see any problems?
Or better: is this implementation considered harmful?
Thanks in advance!
k
Father.pm
Son.pmpackage Father; use warnings; use strict; sub new { my $class = shift; my $self = {}; bless ($self, $class); return $self; } # INTERFACE sub aText { #implement die('Parent aText'); } sub aList { # implement die('Parent aList'); } 1;
test.plpackage Son; use warnings; use strict; use Father; use base qw(Father); use constant { aText => 'This is a Text', aList => [qw(aaa bbb ccc)], }; sub print_son { my $self = shift; print 'Son:', $self->aText(), "\n"; print 'Son: ', join(', ', @{$self->aList()}), "\n"; } 1;
Output:#! /usr/bin/perl use strict; use warnings; use Son; my $s = Son->new(); print $s->aText(), "\n"; print join(', ', @{$s->aList()}), "\n"; $s->print_son();
This is a Text aaa, bbb, ccc Son:This is a Text Son: aaa, bbb, ccc
Back to
Seekers of Perl Wisdom