perlrt

Perl Hook::LexWrap Unable to access return value in post wrapper method


We are customising the Request Tracker (RT) ticketing tool. We want to add certain extra filters to the values returned from SimpleSearch subroutine in RT::Users.

Following Hook::Lexwrap code we have written to access the return value and the arguments from the above subroutine.

Users_Local.pm

package RT::Users;
use strict;
use Hook::LexWrap;
wrap 'SimpleSearch' =>
    post=> sub {
        RT::Logger->info("accessing return value here $_[-1]");
        RT::Logger->info("I got the arguments in post: [@_]");
    },
;
1;

Following is the output:

accessing return value here (/opt/requestTracker/sbin/../local/lib/RT/Users_Local.pm:6)

I am not able to understand why an empty value exists in $_[-1].

Following is the text quoted from http://search.cpan.org/~ether/Hook-LexWrap-0.25/lib/Hook/LexWrap.pm

"In a post-wrapper, $_[-1] contains the return value produced by the wrapped subroutine. In a scalar return context, this value is the scalar return value. In an list return context, this value is a reference to the array of return values. $_[-1] may be assigned to in a post-wrapper, and this changes the return value accordingly."

How can I access the return value from the original subroutine? Any help is appreciated. Thanks


Solution

  • I tested the example code from Hook::LexWrap perldoc and found that $_[-1] is undefined only when wrapped subroutine is called in a void context. So, make sure that you assign return value from your subroutine to some variable.