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
package 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;
Son.pm
package 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;
test.pl
#! /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();
Output:
This is a Text
aaa, bbb, ccc
Son:This is a Text
Son: aaa, bbb, ccc
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.