Post hook overlay

Hello,

I’m trying to add a post hook to the RT::Group::AddMember subroutine. According to this[1], I should be able to do this with a simple Group_Local.pm file, however, nothing happens.

Is it possible to add hooks in the overlay file which calls the original subroutine, then do something before it returns the result of the original subroutine? Or is it only possible with Hook::LexWrap?

Here’s a short version of the code. Note that “FIN: old_sub” and “I am doing stuff” is never printed.

local/lib/RT/Group_Local.pm:

no warnings 'redefine';

my $old_sub = \&RT::Group::AddMember;

sub AddMember {
    my ($id, $msg) = &$old_sub( @_ );

    $RT::Logger->error("FIN: old_sub");

    my $self = shift;
    my $new_member = shift;

    return $id, $msg unless $id;

    $RT::Logger->error("I am doing stuff.");

    return $id, $msg;
}

1;

[1] rt/StyleGuide.pod at stable · bestpractical/rt · GitHub

Hi Thomas,

I believe you need a package RT::Group; statement at the top, otherwise your new AddMember sub is installed into the wrong place.

Best,
Shawn

Hi Shawn,

Thanks for the tip, although I’m sorry to say that it did not work.
Adding the statement package RT::Group; on either the line before or after no warnings 'redefine'; led to endless recursion until the worker process was killed.

Hi,

I ran into the same problem with infinite recursion. I’m not sure if you figured it out, but I’ll post my solution in case someone else comes along with the same problem.

Given the original code I changed it something along the lines of this to make it work (note how AddMember is redefined here):

package RT::Group;
no warnings 'redefine';

my $old_sub = \&RT::Group::AddMember;

*AddMember = sub {
    my ($id, $msg) = &$old_sub( @_ );

    $RT::Logger->error("FIN: old_sub");

    my $self = shift;
    my $new_member = shift;

    return $id, $msg unless $id;

    $RT::Logger->error("I am doing stuff.");

    return $id, $msg;
};

1;

Regards,
Marius