in reply to sub by reference - assigning to an object?

You're trying to assign to a method, which isn't going to work without some deep magic (and even then that's probably not what you want to do). You really want the subref to either be passed as a parameter to the constructor ($parser_name->new( getDBH => \&getDBH )) and/or stored in an attribute ($self->{ 'getDBH_sub' } = \&getDBH) and then the parser instances use that to call the proper routine that way.

Update: So to elaborate, basically each parser class' getDBH routine would look something like this:

sub getDBH { my $self = shift; my $getDBH_ref = $self->{ 'getDBH_sub' }; $getDBH_ref->( ); }

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: sub by reference - assigning to an object?
by ethrbunny (Monk) on Mar 26, 2008 at 16:47 UTC
    What if I want to do something like:

    my $common = common_class->new(); my $parser = parser_class->new(); ### this is the ugly part $parser->{ 'getDBH' } = \&$common->getDBH;

    In my original code this is more like what I was attempting.
      If getDBH() doesn't actually depend on $common and it's state you could do
      $parser->{ 'getDBH' } = \&common_class::getDBH;
      otherwise you could store a wrapper to the getDBH() invocation:
      $parser->{ 'getDBH' } = sub { $common->getDBH; };