autouse(3pm)

πŸ’… autouse(3pm) β€” the "don't load it till you need it" era

okay so bestie, quick vibe check: this is a page from the Perl Programmers Reference Guide, and the module is literally called autouse because it postpones the load of modules until a function is used. iconic behavior honestly. πŸ¦‹


✨ NAME ✨

autouse β€” postpone load of modules until a function is used πŸŽ€


🌸 SYNOPSIS 🌸

  use autouse 'Carp' => qw(carp croak);
  carp "this carp was predeclared and autoused ";

like. that's the whole flex. πŸ’…


πŸ’– DESCRIPTION πŸ’–

so here's the tea β˜• β€” if the module Module is already loaded, then this declaration:

  use autouse 'Module' => qw(func1 func2($;$$));

wait no, keep it exact, it's:

  use autouse 'Module' => qw(func1 func2($;$));

is equivalent to this:

  use Module qw(func1 func2);

...but ONLY if Module defines func2() with prototype ($;$) and func1() has no prototypes. (At least if Module uses Exporter's import β€” otherwise it is a fatal error, no cap, the whole thing just dies. πŸ’€)

now for the other timeline 🌈 β€” if the module Module is not loaded yet, then that same declaration above just declares the functions func1() and func2() in the current package. they're placeholders, main character energy but fake. πŸ‘‘

then, when those functions actually get called, they load the package Module if needed, and substitute themselves with the correct definitions. like a glow-up mid-runtime. ✨


πŸ”₯ WARNING πŸ”₯ (read this one fr fr)

using autouse will move important steps of your program's execution from compile time to runtime. and that can:

  • Break the execution of your program if the module you autoused has some initialization which it expects to be done early. 😭
  • hide bugs in your code since important checks (like correctness of prototypes) is moved from compile time to runtime. In particular, if the prototype you specified on the autouse line is wrong, you will not find it out until the corresponding function is executed. This will be very unfortunate for functions which are not always called (note that for such functions autouseing gives the biggest win β€” for a workaround, see below πŸ‘‡).
bestie this second one is the sneaky one. it's giving "bug that only shows up in prod at 3am." πŸ™ˆ

to alleviate the second problem (partially) it is advised to write your scripts like this:

  use Module;
  use autouse Module => qw(carp($) croak(&$));
  carp "this carp was predeclared and autoused ";

the first line ensures that the errors in your argument specification are found early. ⭐ and when you ship your application you should comment out the first line, since it makes the second one useless. yeah. you read that right. 🫢


🧚 AUTHOR 🧚

Ilya Zakharevich (ilya@math.ohio-state.edu) β€” legend. πŸ‘‘


πŸ¦‹ SEE ALSO πŸ¦‹

perl(1).


and that's the page babes πŸŽ€ β€” straight from perl v5.42.3, dated 2026-11-03, the autouse(3pm) era. go forth and lazily load your modules, but like... carefully. πŸ’œ

πŸ“¦ where this page comes from