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 intosub_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 π
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