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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.