autodie::Util(3pm)

πŸ’… autodie::Util β€” like, the internal utility subs for autodie and Fatal

okay so real talk: this page is autodie::Util(3pm) from the Perl Programmers Reference Guide, and it's the backstage-crew module nobody's supposed to see. 🎭

✨ NAME ✨

autodie::Util β€” Internal Utility subroutines for autodie and Fatal 🫢

✨ SYNOPSIS ✨

# INTERNAL API for autodie and Fatal only!

use autodie::Util qw(on_end_of_compile_scope);
on_end_of_compile_scope(sub { print "Hallo world\n"; });

✨ DESCRIPTION ✨

Internal Utilities for autodie and Fatal! πŸŽ€ Say it with me: this module is not a part of autodie's public API. It's giving "staff only, bestie" energy. πŸšͺ

What lives in here is a lil pile of utility subroutines that abstract away the underlying magic of autodie and the (very much *ab*uses) of "%^H" to call subs at the end of a (compile-time) scope. πŸͺ„

Heads up, and this is important: because of how "%^H" works, some of these utilities are only useful during the compilation phase of a perl module, and they rely on the internals of how perl handles references in "%^H". So like… don't come for me if you use them at runtime. πŸ™ˆ

🌸 Methods 🌸

on_end_of_compile_scope

on_end_of_compile_scope(sub { print "Hallo world\n"; });

Will invoke a sub at the end of a (compile-time) scope. ✨ The sub gets called once, with no arguments. You can call this multiple times β€” even inside the same "compile-time" scope β€” to install multiple subs. And the subs fire in a "first-in-last-out"-order (FILO, aka "stack"-order). Stack behavior, iconic behavior. πŸ“šπŸ’…

fill_protos

fill_protos('*$$;$@')

Give it a Perl subroutine prototype and it hands back a list of invocation specifications. πŸ”₯ Each specification is a listref where:

  • the first member is the (minimum) number of arguments for that invocation specification, and
  • the remaining members are a string representation of how to pass the arguments correctly to a sub with the given prototype, when it's called with that given number of arguments.

The specifications come back in increasing order of arguments, starting at 0 (like ';$') or 1 (like '$@'). πŸ“ˆ

Careful tho: if the prototype is "slurpy" (like, ends with an '@'), then the number of arguments for the last specification is a "minimum" number, not an exact one. You can detect this by checking whether the last member of the last specification matches m/[@#]_/. πŸ•΅οΈβ€β™€οΈ

make_core_trampoline

make_core_trampoline('CORE::open', 'main', prototype('CORE::open'))

Creates a trampoline for calling a core sub. πŸŽͺ Essentially it's a tiny sub that figures out how we should be calling our core sub, puts the arguments in the right way, and bounces our control over to it. Bounce, bounce, slay. 🦘

If we could reliably use `goto &` on core builtins, we wouldn't need this subroutine. But we can't, so here we are. 😭

install_subs

install_subs('My::Module', { 'read' => sub { die("Hallo\n"), ... }})

Give it a package name and a hashref mapping names to a subroutine reference (or "undef"), and this subroutine will install said subroutines under their given name in that module. πŸ› οΈ And if a name maps to "undef", then any subroutine with that name in the target module gets removed β€” which possibly "unshadows" a CORE sub of the same name. πŸ‘‘βœ¨


πŸ¦‹ AUTHOR πŸ¦‹

Copyright 2013-2014, Niels Thykier <niels@thykier.net>

🌈 LICENSE 🌈

This module is free software. You may distribute it under the same terms as Perl itself. 🫢 Legal but make it cute.


and that's the whole page, bestie!! πŸ’– this was autodie::Util(3pm) β€” perl v5.42.3, dated 2026-11-03, brought to you by the Perl Programmers Reference Guide. mwah, go compile something βœ¨πŸŽ€πŸ’…

πŸ“¦ where this page comes from