Perl: implementing a dispatch table in an OO module?

Posted by Iain on Stack Overflow See other posts from Stack Overflow or by Iain
Published on 2010-05-07T21:40:23Z Indexed on 2010/05/07 21:48 UTC
Read the original article Hit count: 431

Filed under:
|
|
|

I want to put some subs that are within an OO package into an array - also within the package - to use as a dispatch table. Something like this

package Blah::Blah;

use fields 'tests';

sub new {
    my($class )= @_;

my $self = fields::new($class);

    $self->{'tests'} = [
                         $self->_sub1
                        ,$self->_sub2
                       ];
    return $self;
}

_sub1 { ... };
_sub2 { ... };

I'm not entirely sure on the syntax for this?

$self->{'tests'} = [
                         $self->_sub1
                        ,$self->_sub2
                       ];

or

$self->{'tests'} = [
                         \&{$self->_sub1}
                        ,\&{$self->_sub2}
                       ];

or

$self->{'tests'} = [
                         \&{_sub1}
                        ,\&{_sub2}
                       ];

I don't seem to be able to get this to work within an OO package, whereas it's quite straightforward in a procedural fashion, and I haven't found any examples for OO.

Any help is much appreciated, Iain

© Stack Overflow or respective owner

Related posts about perl

Related posts about dispatch