Implementing the Command pattern in Perl adequately really shouldn't require much work as it doesn't depend on qualities that one must coerce Perl into exhibiting, like private variables, or private subroutines without resorting to closures1.
For this pattern however there is a particularly promising implementation available in Perl due to the fact that almost anything can be an object, including a subroutine. For example:
package Action;
sub new {
my ($class, %args) = @_;
my $coderef = sub { while (my ($k, $v) = each(%args)) { ... } };
return bless($coderef, $class);
}
sub foo {
$_[0]->();
}
In this case, when foo() is invoked the first element of @_ contains a reference to the blessed subroutine, which may then be called by dereferencing. More complex versions of foo() might allow parameters to be passed to the subroutine to vary behavior in whatever way you need, or the module might define additional methods that call the subroutine in various ways. Take a look at TheDamian's Object Oriented Perl chapter 5.2 for (much) more.
And now a word of caution. It sounds like this is a first draft of a very complicated system. I think it's highly likely that you'll discover as you go many many problems and requirements that you had not anticipated. And though patterns can yield great elegance and capability, if implemented without a clear vision they will fast begin to feel like concrete shoes. (see Design Patterns Considered Harmful) I strongly suggest a prototype free of patterns. Once you've thoroughly mapped out what the system requirements are, then go back and rewrite using patterns (if they're still applicable at that point).
- Sometimes with design patterns translating between languages can be difficult due to differences in the design of the language. Take the Singleton pattern -- in C++ it's performed using a private static variable to ensure that only one instance of the class ever exists, a private constructor, and a single public class method to control creation and subsequent access. In Perl to get equivalent functionality you'd need to make use of persistent lexically scoped variables, as in:
package SingletonObject;
{
my $instance;
sub instance
{
unless (defined $instance) {
$instance = bless ({}, $class);
# other initialization stuff
}
return $instance;
}
}
Update: Fixed a couple of typos, grammatical errors.
| [reply] [d/l] [select] |
Your requirements for upgrading aren't Perl specific.
Looking for a Perl specific answer isn't the right way of
going. There are several package managers out there that
will have the functionality that you require. Unfortunally,
package managers tend to be very non-portable. Suns package
manager will do what you want it to do, and so will HP's
swinstall. There's a entire army of package
managers for Linux systems, but whether they will have all
the functionality that you require, I cannot answer.
Abigail | [reply] [d/l] |