Test2::Util(3perl)
β¨ Test2::Util β¨
okay bestie, welcome to Test2::Util π this lil page lives in the Perl Programmers Reference Guide, and it's basically the toolbox that Test2 and all its friends keep in their backpack π
πΈ NAME πΈ
Test2::Util β Tools used by Test2 and friends. that's it, that's the vibe π«Ά
π¦ DESCRIPTION π¦
It's a whole collection of tools that Test2 and friends use. A grab-bag, a little utility pouch, a miscellaneous drawer full of things that make testing not a nightmare. periodt π
π EXPORTS π
big news: every single export here is optional. nothing just shows up in your namespace uninvited β you have to specify the subs you want to import. consent-based imports, we love to see it πβ¨
($success, $error) = try { ... }β runs that codeblock and hands you back whether it succeeded and the error message. the glow-up here is that it protects$@and$!β they get restored by the time the run ends, untouched. it also temporarily blocks out$SIG{DIE}handlers while it's doing its thing. protective king πprotect { ... }β kinda liketry, except it does NOT catch exceptions. no catching, no saving you from the fall π the whole point is guarding$@and$!from getting messed with. if the run succeeds, both$@and$!go right back to whatever they were before. if the run fails,$!still gets restored, but$@will hold the exception that got thrown. so, y'know, it's giving "I saved what I could" πCAN_FORKβ true if this system can do true fork or pseudo-fork. it's capable, bestie πͺCAN_REALLY_FORKβ true only if the system can really fork. for systems where fork is emulated, this one's gonna be false. real ones only πCAN_THREADβ true if this system can use threads. threading ability check βUSE_THREADSβ returns true if threads are enabled, false if they're not. straight to the point, no notes πget_tidβ gives you the id of the current thread when threads are enabled. when they're not? it just returns0. gotcha, babe πmy $file = pkg_to_file($package)β takes a package name and turns it into a filename. glow-up: package β path β¨$string = ipc_separator()β hands you the IPC separator. right now it's always the string'~'. that's just the way it is, no cap π$string = gen_uid()β generates a unique id. and no, it is NOT A UUID, don't come for me π it's typically the process id, the thread id, the time, and an incrementing integer, all joined up withipc_separator().
these IDs are unique enough for most purposes. for two identical ids to happen, you'd need 2 processes with the same PID generating IDs at the same time with the same current state of the incrementing integer. across multiple machines that's a perfectly reasonable thing to expect to happen β on one machine, it's quite unlikely. so like, chill π
but listen, there are ways it can fail to be unique:
- a process generates an id, calls
exec, and does it again after theexec, and the whole thing happens in less than a second - the system's process ids cycle in less than a second, letting 2 different programs that use this generator run with the same PID in under a second
both of these cases are sufficiently unlikely, so we're fine fr fr π«Ά BUT. if you need universally unique ids, or ids that stay unique under those conditions, go look at Data::UUID. different tool, different job π
($ok, $err) = do_rename($old_name, $new_name)β renames a file. it's a wrapper aroundrename()that makes things more reliable cross-platform when you're trying to rename files you recently altered π§($ok, $err) = do_unlink($filename)β unlinks a file. same deal, it wrapsunlink()so it's more reliable cross-platform when you're unlinking files you recently altered π
π NOTES && CAVEATS π
πΈ 5.10.0
Perl 5.10.0 has a bug when it's compiled with newer gcc versions. and this bug is mean β it causes a segfault whenever a new thread is launched. π Test2 will try to detect this, and when it does detect it, it'll note that the system is not capable of forking. so don't blame Test2, blame the bug bestie π
π Devel::Cover
Devel::Cover does not support threads. period. so CAN_THREAD will return false if Devel::Cover is loaded before the check is first run. order matters, iykyk π
β SOURCE β
the source code repository for Test2 is right here, bestie:
<https://github.com/Test-More/test-more/>
π 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 <https://dev.perl.org/licenses/>
okay that's the whole page, you ate that π«Ά this was perl v5.44.0, dated 2026-10-17, and yes babe, it's Test2::Util(3perl) β go forth and import responsibly π
β¨
π¦ 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