Test2::Util::Sub(3)

πŸ“¦ version 1.302214, in opensuse-leap 16.0

πŸ’– NAME πŸ’–

okay bestie, this is the manual page for Test2::Util::Sub β€” that's Test2::Util::Sub(3), a User Contributed Perl Documentation page, and it's literally Tools for inspecting and manipulating subs. No cap. πŸ’…

✨ DESCRIPTION ✨

Utilities used by Test2::Tools to inspect and manipulate subroutines. That's the whole vibe: the helper utilities that Test2::Tools uses when it needs to look at subs and do stuff to them. Obsessed. 🫢

πŸŽ€ EXPORTS πŸŽ€

All exports are optional, you must specify subs to import. Like, none of this auto-imports sneakily into your namespace β€” if you want these, name them. Periodt. πŸ’€

$name = sub_name(\&sub)

Get the name of the sub. Simple, iconic, does exactly what it says. ✨

my $hr = sub_info(\&code)

This returns a hashref with information about the sub:

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

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

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

Here's the breakdown, bestie:

  • $info->{ref} => \&code β€” this is the original sub passed to sub_info(). It's the coderef you handed in, no cap. 🫢
  • $info->{cobj} => $cobj β€” this is the c-object representation of the coderef. Very internal, very iconic. 🧚
  • $info->{name} => "Some::Mod::code" β€” this is the name of the coderef. For anonymous coderefs this may end with __ANON__. Also note that the package 'main' is special, and 'main::' may be omitted. Iykyk. πŸ’…
  • $info->{file} => "Some/Mod.pm" β€” the file in which the sub was defined. πŸŽ€
  • $info->{package} => "Some::Mod" β€” the package in which the sub was defined. Slay. ✨
  • $info->{start_line} => 22, $info->{end_line} => 42, $info->{lines} => [22, 42] β€” these three fields are the adjusted start line, end line, and array with both. Important: 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.

  • $info->{all_lines} => [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. Lowkey important. πŸ¦‹

🌈 SOURCE 🌈

The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/. Go click, bestie. πŸ’–

πŸ‘‘ MAINTAINERS πŸ‘‘

Chad Granum <exodist@cpan.org> πŸ‘‘

🌟 AUTHORS 🌟

  • Chad Granum <exodist@cpan.org>
  • Kent Fredric <kentnl@cpan.org>

Copyright Chad Granum <exodist@cpan.org>.

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

See http://dev.perl.org/licenses/


and that's a wrap, bestie β€” this page comes from perl v5.42.0, dated 2025-07-28, and it's filed as Test2::Util::Sub(3). Slay. πŸ’…βœ¨

πŸ“¦ where this page comes from