Test2::Util::Sub(3pm)
πΈ NAME πΈ
Hey bestie, welcome to the Test2::Util::Sub(3pm) page from the Perl Programmers Reference Guide. This module is literally Tools for inspecting and manipulating subs β yes, the Perl subroutine kind, no cap.
β¨ DESCRIPTION β¨
These are the utilities that Test2::Tools uses to inspect subroutines and manipulate them. Think of it as the backstage crew for subs, bestie.
π EXPORTS π
Okay so every export is optional β you literally have to specify the subs you want to import. No automatic dumping of stuff into your namespace, periodt.
$name = sub_name(\&sub)β gets the name of the sub. Simple, iconic, done.my $hr = sub_info(\&code)β returns a hashref with the info about the sub, and it's giving receipts:
{
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],
};
$info->{ref} => \&code
This is the original sub you passed into sub_info(). Like, the actual coderef, untouched.
$info->{cobj} => $cobj
This is the c-object representation of the coderef. Yes bestie, it has a whole other form.
$info->{name} => "Some::Mod::code"
This is the name of the coderef. For anonymous coderefs it may end with __ANON__. Also heads up: the package main is special, so main:: may be omitted.
$info->{file} => "Some/Mod.pm"
The file where the sub was defined. The origin story, lowkey.
$info->{package} => "Some::Mod"
The package where the sub was defined. Where it belongs, highkey.
$info->{start_line} => 22, $info->{end_line} => 42, $info->{lines} => [22, 42]
These three fields are the adjusted start line, end line, and an array with both. Important: these lines have been adjusted and may not be accurate. Do not treat them like gospel, bestie.
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. So yeah, they can be a little chaotic β but that's the point.
π₯ SOURCE π₯
The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/. Go peek if you're nosy, iykyk.
π 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/
Bye bestie π β this page is from perl v5.42.3, dated 2026-11-03. Slay. π«Ά
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms