<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::Util::Sub;
use strict;
use warnings;

our $VERSION = '0.000162';

use Carp qw/croak carp/;
use B();

our @EXPORT_OK = qw{
    sub_info
    sub_name

    gen_reader gen_writer gen_accessor
};
use base 'Exporter';

sub gen_reader {
    my $field = shift;
    return sub { $_[0]-&gt;{$field} };
}

sub gen_writer {
    my $field = shift;
    return sub { $_[0]-&gt;{$field} = $_[1] };
}

sub gen_accessor {
    my $field = shift;
    return sub {
        my $self = shift;
        ($self-&gt;{$field}) = @_ if @_;
        return $self-&gt;{$field};
    };
}

sub sub_name {
    my ($sub) = @_;

    croak "sub_name requires a coderef as its only argument"
        unless ref($sub) eq 'CODE';

    my $cobj = B::svref_2object($sub);
    my $name = $cobj-&gt;GV-&gt;NAME;
    return $name;
}

sub sub_info {
    my ($sub, @all_lines) = @_;
    my %in = map {$_ =&gt; 1} @all_lines;

    croak "sub_info requires a coderef as its first argument"
        unless ref($sub) eq 'CODE';

    my $cobj    = B::svref_2object($sub);
    my $name    = $cobj-&gt;GV-&gt;NAME;
    my $file    = $cobj-&gt;FILE;
    my $package = $cobj-&gt;GV-&gt;STASH-&gt;NAME;

    my $op = $cobj-&gt;START;
    while ($op) {
        push @all_lines =&gt; $op-&gt;line if $op-&gt;can('line');
        last unless $op-&gt;can('next');
        $op = $op-&gt;next;
    }

    my ($start, $end, @lines);
    if (@all_lines) {
        @all_lines = sort { $a &lt;=&gt; $b } @all_lines;
        ($start, $end) = ($all_lines[0], $all_lines[-1]);

        # Adjust start and end for the most common case of a multi-line block with
        # parens on the lines before and after.
        if ($start &lt; $end) {
            $start-- unless $start &lt;= 1 || $in{$start};
            $end++   unless $in{$end};
        }
        @lines = ($start, $end);
    }

    return {
        ref        =&gt; $sub,
        cobj       =&gt; $cobj,
        name       =&gt; $name,
        file       =&gt; $file,
        package    =&gt; $package,
        start_line =&gt; $start,
        end_line   =&gt; $end,
        all_lines  =&gt; \@all_lines,
        lines      =&gt; \@lines,
    };
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Test2::Util::Sub - Tools for inspecting and manipulating subs.

=head1 DESCRIPTION

Utilities used by Test2::Tools to inspect and manipulate subroutines.

=head1 EXPORTS

All exports are optional, you must specify subs to import.

=over 4

=item $name = sub_name(\&amp;sub)

Get the name of the sub.

=item my $hr = sub_info(\&amp;code)

This returns a hashref with information about the sub:

    {
        ref        =&gt; \&amp;code,
        cobj       =&gt; $cobj,
        name       =&gt; "Some::Mod::code",
        file       =&gt; "Some/Mod.pm",
        package    =&gt; "Some::Mod",

        # Note: These have been adjusted based on guesswork.
        start_line =&gt; 22,
        end_line   =&gt; 42,
        lines      =&gt; [22, 42],

        # Not a bug, these lines are different!
        all_lines  =&gt; [23, 25, ..., 39, 41],
    };

=over 4

=item $info-&gt;{ref} =&gt; \&amp;code

This is the original sub passed to C&lt;sub_info()&gt;.

=item $info-&gt;{cobj} =&gt; $cobj

This is the c-object representation of the coderef.

=item $info-&gt;{name} =&gt; "Some::Mod::code"

This is the name of the coderef. For anonymous coderefs this may end with
C&lt;'__ANON__'&gt;. Also note that the package 'main' is special, and 'main::' may
be omitted.

=item $info-&gt;{file} =&gt; "Some/Mod.pm"

The file in which the sub was defined.

=item $info-&gt;{package} =&gt; "Some::Mod"

The package in which the sub was defined.

=item $info-&gt;{start_line} =&gt; 22

=item $info-&gt;{end_line} =&gt; 42

=item $info-&gt;{lines} =&gt; [22, 42]

These three fields are the I&lt;adjusted&gt; start line, end line, and array with both.
It is important to note that these lines have been adjusted and may not be
accurate.

The lines are obtained by walking the ops. As such, the first line is the line
of the first statement, and the last line is the line of the last statement.
This means that in multi-line subs the lines are usually off by 1.  The lines
in these keys will be adjusted for you if it detects a multi-line sub.

=item $info-&gt;{all_lines} =&gt; [23, 25, ..., 39, 41]

This is an array with the lines of every statement in the sub. Unlike the other
line fields, these have not been adjusted for you.

=back

=back

=head1 SOURCE

The source code repository for Test2-Suite can be found at
F&lt;https://github.com/Test-More/Test2-Suite/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=item Kent Fredric E&lt;lt&gt;kentnl@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2018 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

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

See F&lt;http://dev.perl.org/licenses/&gt;

=cut
</pre></body></html>