The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Sub::Spy - Sub::Spy is subref wrapper that records arguments, return value, and exception thrown.

VERSION

This document describes Sub::Spy version 0.01.

SYNOPSIS

    use Sub::Spy qw/spy inspect/;

    my $subref = sub { return $_[0] * $_[1]; };
    my $spy = spy($subref);

    $spy->(2, 5);
    my $i = inspect($spy);

    $i->called; # 1 (true)
    $i->called_once; # 1 (true)

    $i->args; # [[2, 5]]
    $i->return_values; # [10]

    $i->get_args(0); # [2, 5]
    $i->get_return_value(0); # 10


    $spy->(3, 3);

    $i->called_twice; # 1 (true)

    $i->get_call(0)->args; # [2, 5]
    $i->get_call(0)->return_value; # 10

    $i->get_call(1)->args; # [3, 3]
    $i->get_call(1)->return_value; # 9

DESCRIPTION

Sub:Spy provies the way to inspect each subref calls. It might be useful for testing callback-style interface or asyncronous subref call. (e.g. AnyEvent)

FUNCTIONS

spy($subref)

returns wrapped subref as 'spy'

inspect($spy)

inspect the 'spy' subref and returns Sub::Spy::Result instance

INTERFACE of Sub::Spy::Result

get_call(i)

returns Sub::Spy::Call instance which represents (i+1)th call

call_count

returns how many times subref called

called

returns 1 if subref called at least once

called_once

returns 1 if subref called just once

called_twice

returns 1 if subref called just twice

called_thrice

returns 1 if subref called just thrice

called_times(n)

returns 1 if subref called just n times

args

returns arguments that passed to subref

get_args(i)

returns arguments that passed to subref at (i+1)th call

exceptions

returns exceptions that raised in subref

get_exception(i)

returns exceptions that raised in subref at (i+1)th call

threw

returns 1 if exception raised in subref at least once

return_values

returns return values that subref yield

get_return_value(i)

returns return value that subref yield at (i+1)th call

INTERFACE of Sub::Spy::Call

args

returns arguments at that call

exception

returns exception raised at that call

threw

returns 1 if exception raised at that call

return_value

returns return value yielded at that call

DEPENDENCIES

Perl 5.8.1 or later.

BUGS

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.

SEE ALSO

perl

AUTHOR

Naosuke Yokoe <yokoe.naosuke@dena.jp>

LICENSE AND COPYRIGHT

Copyright (c) 2011, Naosuke Yokoe. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.