TEST2::UTIL::SUB(3perl)

πŸ’– NAME πŸ’–

ok so Test2::Util::Sub β€” it's literally a little toolbox for peeking at and messing with subs (subroutines, bestie, keep up πŸ’…). It lives in the Test2::Util::Sub(3perl) corner of the Perl universe.


✨ DESCRIPTION ✨

These are the utilities that Test2::Tools uses to inspect and manipulate subroutines. That's the whole vibe. It's giving backstage-pass energy for your coderefs πŸŽ€.


🫢 EXPORTS 🫢

okay so every single export here is optional β€” nothing jumps into your namespace uninvited, you have to specify which subs you want to import. consent is key, periodt ✨.

  • $name = sub_name(\&sub) β€” bestie, this hands you the name of the sub. That's it. That's the whole thing. πŸ‘‘
  • my $hr = sub_info(\&code) β€” this one returns a hashref stuffed with all the deets 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],
};

and now let's do a lil tour of every key, because each one is a whole personality 🌸:

  • $info->{ref} => \&code β€” this is the original sub you passed into sub_info(). the source material, the blueprint, the main character πŸ’œ.
  • $info->{cobj} => $cobj β€” this is the c-object representation of that coderef. very "under the hood", very ykyk πŸ”₯.
  • $info->{name} => "Some::Mod::code" β€” the name of the coderef! but okay heads up: for anonymous coderefs this might end with '__ANON__'. also, the package 'main' is special, so 'main::' may just be omitted, no cap πŸ™ˆ.
  • $info->{file} => "Some/Mod.pm" β€” the file where the sub got defined. its origin story πŸ¦‹.
  • $info->{package} => "Some::Mod" β€” the package where the sub got defined. where it grew up πŸ’–.
  • $info->{start_line} => 22
  • $info->{end_line} => 42
  • $info->{lines} => [22, 42] β€” these three are the adjusted start line, end line, and an array holding both of them. okay listen, this matters: these lines have been adjusted and they might not be accurate, fr fr 😭.

how do they even get these? by walking the ops, bestie. so the first line is the line of the first statement, and the last line is the line of the last statement. which means in multi-line subs the lines are usually off by 1 πŸ’€. but don't stress β€” the lines in these three keys get adjusted for you automatically if it detects a multi-line sub 🧚.

  • $info->{all_lines} => [23, 25, ..., 39, 41] β€” an array with the lines of every single statement in the sub. but unlike those other line fields, these have not been adjusted for you. you're on your own with this one, gorgeous πŸ’….
not a bug, bestie β€” those lines are supposed to be different. read it again. i said what i said. πŸ’€

🌈 SOURCE 🌈

the source code repository for Test2-Suite can be found at:

https://github.com/Test-More/test-more/

go look, it's iconic πŸ‘‘


πŸ‘‘ 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 the whole page, babes πŸŽ€βœ¨ β€” this was perl v5.44.0, dated 2026-10-17, and the page is Test2::Util::Sub(3perl). mwah, go inspect some subs πŸ’…πŸ«Ά

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms