Test2::Util(3pm)

πŸ’… Test2::Util β€” the group chat version πŸ’…

ok so this is Test2::Util, straight out of the Perl Programmers Reference Guide (section 3pm for those keeping score πŸŽ€). She's the utility drawer that Test2 and all her friends keep raiding.


✨ NAME ✨

Test2::Util β€” tools used by Test2 and friends. that's it, that's the vibe 🫢


🌸 DESCRIPTION 🌸

literally just a collection of tools used by Test2 and friends. a little grab-bag of helpfulness. iconic behavior, no notes πŸ’–


πŸ¦‹ EXPORTS πŸ¦‹

ok so all exports are optional β€” nothing jumps into your namespace uninvited, that's rude. you must specify which subs you want to import. consent first, always ✨

  • ($success, $error) = try { ... } β€” evals the codeblock and gives you back success-or-failure plus the error message. bestie, this one protects $@ and $! for you β€” they get restored by the end of the run. it also temporarily blocks $SIG{DIE} handlers. very protective, very demure πŸ’…
  • protect { ... } β€” similar to try, except it does not catch exceptions. the whole point here is to shield $@ and $! from changes. $@ and $! get restored to whatever they were before the run, as long as the run is successful. if the run fails, $! still gets restored, but $@ will contain the exception being thrown. so like, one of them's coming back either way, but $@ snitches on failure 😭
  • CAN_FORK β€” true if this system is capable of true or pseudo-fork. slay πŸ”₯
  • CAN_REALLY_FORK β€” true if the system can really fork. this will be false for systems where fork is emulated. it's giving "fake it till you make it" but the test knows πŸ’€
  • CAN_THREAD β€” true if this system is capable of using threads. πŸ‘‘
  • USE_THREADS β€” returns true if threads are enabled, false if they are not. simple as that 🧚
  • get_tid β€” returns the id of the current thread when threads are enabled, otherwise it returns 0. lowkey the most chill function here 🌈
  • my $file = pkg_to_file($package) β€” converts a package name to a filename. glow-up for your strings ✨
  • $string = ipc_separator() β€” gets the IPC separator. currently this is always the string '~'. that's her whole personality and we respect it πŸŽ€
  • $string = gen_uid() β€” generates a unique id (NOT A UUID, she said what she said). this will typically be the process id, the thread id, the time, and an incrementing integer, all joined with the ipc_separator().

these ID's are unique enough for most purposes. for identical ids to be generated you must have 2 processes with the same PID generate IDs at the same time with the same current state of the incrementing integer. this is a perfectly reasonable thing to expect to happen across multiple machines, but it's quite unlikely to happen on one machine.

⚠️ heads up bestie, this can fail to be unique if a process generates an id, calls exec, and does it again after the exec and it all happens in less than a second. it can also happen if the systems process id's cycle in less than a second, allowing 2 different programs that use this generator to run with the same PID in less than a second. both these cases are sufficiently unlikely. if you need universally unique ids, or ids that are unique in these conditions, look at Data::UUID. periodt πŸ’…
  • ($ok, $err) = do_rename($old_name, $new_name) β€” renames a file. this wraps rename() in a way that makes it more reliable cross-platform when you're trying to rename files you recently altered. she's got your back on every OS 🫢
  • ($ok, $err) = do_unlink($filename) β€” unlinks a file. this wraps unlink() in a way that makes it more reliable cross-platform when you're trying to unlink files you recently altered. same energy, different syscall πŸ¦‹

πŸ’€ NOTES && CAVEATS πŸ’€

5.10.0

Perl 5.10.0 has a bug when compiled with newer gcc versions. this bug causes a segfault whenever a new thread is launched 😭. Test2 will attempt to detect this, and note that the system is not capable of forking when it's detected. not me debugging a segfault at 3am πŸ•’

Devel::Cover

Devel::Cover does not support threads. CAN_THREAD will return false if Devel::Cover is loaded before the check is first run. so like, load order matters, bestie πŸ™ˆ


🌟 SOURCE 🌟

the source code repository for Test2 can be found at <https://github.com/Test-More/test-more/>. go star it, she deserves it ⭐


πŸ‘‘ 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 <https://dev.perl.org/licenses/> 🌸


and that's the whole page, bestie β€” brought to you by perl v5.42.3, dated 2026-11-03, which is the version and date of this little masterpiece. mwah πŸ’‹ slay and happy testing πŸŽ€

πŸ“¦ where this page comes from