Good morning monks!
I just ran into a "problem" with OO coding and I'm sure there's some elegant solution out there. So let's give it a try:
I got a Module
Anything with a couple of methods. Now I want to have lists of
Anything objects as
Anything::List objects and I want to call the
Anything methods on all objects in a
Anything::List in a row.
That's easy of course but there are a lot of methods in
Anything and all their
Anything::List equivalents look exactly the same and thats actually bothering me!
And here is a snippet for demonstration:
package Anything;
sub new {
my $class = shift;
## ... code here
bless $self, $class;
}
sub prepare_create {
## .. do something
}
sub create {
## .. do something else
}
## a.s.o. more methods here
package Anything::List;
sub new {
my $class = shift;
my @self = @_;
bless \@self, $class;
}
sub add {
my $self = shift;
push @$self, @_;
}
sub list {
my $self = shift;
return @$self;
}
sub prepare_create {
my $self = shift;
foreach my $anything (@{$self}) {
$anything->prepare_create(@_);
}
}
sub create {
my $self = shift;
foreach my $anything (@{$self}) {
$anything->create(@_);
}
}
Thanks for some inspiring hints!
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.