base(3pm)

πŸ’… base(3pm) β€” like, the Perl Programmers Reference Guide one πŸŽ€

okay so bestie, this is base, from the Perl Programmers Reference Guide, and it's about establishing an ISA relationship with base classes at compile time. yes that's the whole name, we're keeping it, periodt.

✨ SYNOPSIS ✨

package Baz;
use base qw(Foo Bar);

like, that's it. that's the whole vibe. πŸ¦‹

🌸 DESCRIPTION 🌸

okay so first thing, real talk: unless you're using the fields pragma, this module is discouraged β€” as in, consider using the lighter-weight parent instead. ngl, parent is kind of the moment. πŸ’…

but what does base even do? it lets you both load one or more modules and set up inheritance from those modules at the same time. two things, one line, main character energy. ✨

roughly similar in effect to this whole thing:

package Baz;
BEGIN {
    require Foo;
    require Bar;
    push @ISA, qw(Foo Bar);
}

so when base tries to require a module, it will not die if it can't find the module's file β€” cute β€” but it will die on any other error. 😭

after all that, should your base class be empty, containing no symbols, base will die. and weirdly that's actually useful! it's for inheriting from classes in the same file as yourself but where the filename does not match the base module name, like so:

        # in Bar.pm
        package Foo;
        sub exclaim { "I can have such a thing?!" }

        package Bar;
        use base "Foo";

see what's happening? there is no Foo.pm, but because Foo defines a symbol (that exclaim subroutine), base will not die when the require fails to load Foo.pm. iconic behavior, honestly. πŸ‘‘

base will also initialize the fields if one of the base classes has it. but listen β€” multiple inheritance of fields is NOT supported. if two or more base classes each have inheritable fields, the 'base' pragma will croak. 🫠 see fields for a description of this feature.

also important: the base class' import method is not called. like, not at all. don't @ me.

πŸ”₯ DIAGNOSTICS πŸ”₯

  • Base class package "%s" is empty. base.pm was unable to require the base package, because it was not found in your path. πŸ’€
  • Class 'Foo' tried to inherit from itself attempting to inherit from yourself generates a warning. yeah bestie, self-inheritance is not the flex you think it is πŸ™ˆ
package Foo;
use base 'Foo';

⭐ HISTORY ⭐

this module was introduced with Perl 5.004_04. vintage. πŸŽ€

πŸ¦‹ CAVEATS πŸ¦‹

due to the limitations of the implementation, you must use base before you declare any of your own fields. like, before. not after. order matters, iykyk. ⚠️

🌈 SEE ALSO 🌈

  • fields

and that's the tea, bestie πŸ’– β€” this was base(3pm) from perl v5.42.3, dated 2026-11-03. go forth and inherit responsibly, ily 🫢✨

πŸ“¦ where this page comes from