Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

xsawyerx (8978)

xsawyerx
  (email not shown publicly)

Journal of xsawyerx (8978)

Monday December 22, 2008
10:47 AM

Extending with Moose

[ #38132 ]
I need to extend a POE module and add some crucial functionality in order to use it in an enterprise production environment. Using Moose for this makes it so easy I'm willing to give up the extra few seconds it takes for compilation.

Basically it took me a while to figure out how I want to extend it. I thought about adding a file to the project and repackage it as a distribution (obviously wouldn't go up on CPAN in that way) but that was too much work (Perl programmers like to be lazy). Then I thought about hacking the source directly but that's a given "no no". Alright, so hmm.. I can easily override a function, replicate it and then add/change it accordingly. This, however, isn't a good idea. Whenever you have to copy stuff, you've probably made a design error.

Fortunately, Moose lets me just add code at the end of a function. Works like this:

use YAML qw( LoadFile );
use Moose;
extends 'Whatever';

after '_something' => sub {
    my $self = shift;

    if ( my $yaml_conf = $self->{'yaml_conf'} ) {
        $self->{'yaml_data'} = LoadFile($yaml_conf);
    }
};

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.