Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Using constant to override (abstract) methods. by klekker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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 or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2022-01-25 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?