DEVEL::SELFSTUBBER(3pm)

πŸ’… NAME πŸ’…

Devel::SelfStubber β€” bestie, it literally generates stubs for a SelfLoading module, no cap ✨


πŸŽ€ SYNOPSIS πŸŽ€

Okay so if you just want the stubs and nothing else, you do this:

use Devel::SelfStubber;
Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');

Or β€” and this is the glow-up version β€” if you want the whole module with the stubs slotted in exactly where they belong:

use Devel::SelfStubber;
$Devel::SelfStubber::JUST_STUBS=0;
Devel::SelfStubber->stub('MODULENAME','MY_LIB_DIR');

Quick vibe check on MODULENAME πŸ‘‘: it's the Perl module name, like Devel::SelfStubber β€” NOT 'Devel/SelfStubber' and NOT 'Devel/SelfStubber.pm'. Those are not the same thing, bestie, don't do it πŸ’€

And MY_LIB_DIR? If you don't pass it, it just defaults to '.'. Easy. 🫢


✨ DESCRIPTION ✨

So Devel::SelfStubber prints out the stubs you need to drop into your module before the __DATA__ token. (Or, if you're feeling fancy, you can make it print the entire module with the stubs placed correctly.) What do the stubs even do? They make sure that if a method gets called, it actually gets loaded. And they're needed specifically for inherited autoloaded methods. Like, that's the whole point. 🎯

The best way to explain this is with the classic example, so buckle up πŸ¦‹

Assume four classes: A, B, C & D.

A is the root class, B is a subclass of A, C is a subclass of B, and D is another subclass of A.

                        A
                       / \
                      B   D
                     /
                    C

Now watch this. If D calls an autoloaded method 'foo' which is defined in class A, then the method gets loaded into class A, then executed. Fine, slay. πŸ’…

But then if C calls method 'foo', and that method was reimplemented in class B but set to be autoloaded β€” oh no β€” the lookup mechanism never gets to the AUTOLOAD mechanism in B, because it first finds the method already loaded in A, and so erroneously uses that one. 😭 Wrong sub, bestie!

If the method foo had been stubbed in B though? The lookup mechanism would have found the stub, and correctly loaded and used the sub from B. Periodt. ✨

So for classes and subclasses to have inheritance correctly work with autoloading, you need to ensure stubs are loaded. That's the whole assignment. πŸ“

The SelfLoader can load stubs automatically at module initialization with the statement 'SelfLoader->load_stubs()'; β€” but you may wish to avoid having the stub loading overhead associated with your initialization. (Though lowkey, note that the SelfLoader::load_stubs method will be called sooner or later β€” at latest when the first sub is being autoloaded.) πŸ™ˆ

In that case, you can put the sub stubs before the __DATA__ token. You could do this manually, but honestly? Why would you. This module lets you generate the stubs automatically. Main character energy. πŸ‘‘

By default it just prints the stubs. But if you set the global $Devel::SelfStubber::JUST_STUBS to 0, it will print out the entire module with the stubs positioned correctly. Highkey useful. πŸ”₯

And at the very least, this is useful to see what the SelfLoader thinks are stubs β€” because, get this, in order to ensure future versions of the SelfStubber remain in step with the SelfLoader, the SelfStubber actually uses the SelfLoader to determine which stubs are needed. Iconic circular queen behaviour, iykyk. πŸ’œ


🌸 Ok that's the whole page bestie, mwah 🫢 β€” this was Devel::SelfStubber(3pm), from perl v5.42.3, dated 2026-11-03, part of the Perl Programmers Reference Guide. Go stub something cute πŸŽ€βœ¨

πŸ“¦ where this page comes from