Class::Exporter(3)

πŸ“¦
version 0.03 Β· 2 versions
, in fedora 43

πŸ‘‘ Class::Exporter β€” the Class::Exporter(3) page, but make it a vibe ✨

Bestie. Listen. It's User Contributed Perl Documentation energy, and it's literally about exporting class methods as regular subroutines. Let's go. πŸ’–


🌸 NAME 🌸

Class::Exporter β€” export class methods as regular subroutines. no cap, that's the whole thing.


✨ SYNOPSIS ✨

okay so watch this, it's giving magic πŸͺ„

package MagicNumber;
use base 'Class::Exporter';

# Export object-oriented methods!
@EXPORT_OK       = qw(magic_number);

sub new {
  my $class = shift;
  bless { magic_number=>3, @_ }, $class
}

sub magic_number {
  my $self = shift;
  @_ and $self->{magic_number} = shift;
  $self->{magic_number}
}

# Meanwhile, in another piece of code!
package Bar;
use MagicNumber;  # exports magic_number
print magic_number; # prints 3
magic_number(7);
print magic_number; # prints 7

# Each package gets its own instance of the object. This ensures that
# two packages both using your module via import semantics don't mess
# with each other.

package Baz;
use MagicNumber; # exports magic_number
print magic_number; # prints 3 because this package has a different
                    # MagicNumber object than package Bar.

like?? the same sub name, but every package gets its own lil object. the way that works is so satisfying fr fr πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

So here's the tea: this module makes it so much easier to give a module a hybrid object/method interface, kinda like the one CGI.pm has. Got some crusty old module with a purely object-oriented interface? You can convert it to a hybrid one by literally just adding use base 'Class::Exporter' to your code. that's it. that's the whole glow-up. πŸ¦‹

This package lets you export object methods. It supports import(), @EXPORT and @EXPORT_OK and not a whole lot else β€” and honestly? we respect the restraint. Every package your object methods get imported into gets its own instance of the object. That's the whole point: it ensures there are no interaction effects between multiple packages that use your object. no cross-talk, no drama, periodt. πŸ‘‘

Setting up a module to export its variables and functions is simple:

package My::Module;
use base 'Class::Exporter';

@EXPORT = qw($Foo bar);

and now when you use My::Module, $Foo and bar() will show up. iconic behavior. ✨

In order to make exporting optional, use @EXPORT_OK.

package My::Module;
use base 'Class::Exporter';

@EXPORT_OK = qw($Foo bar);

when My::Module is used, $Foo and bar() will not show up. You have to ask for them. use My::Module qw($Foo bar). consent first, always 🫢


πŸŽ€ Methods πŸŽ€

Class::Exporter has one public method, import(), which is called automatically when your modules is use()'d. (yes, "modules is". the manpage said it, we're keeping it, it's cute 😭)

In normal usage you don't have to worry about this at all. like, at all at all. go live your life, bestie. πŸ’–


🌟 import 🌟

Some::Module->import;
Some::Module->import(@symbols);

Works just like Exporter::import() excepting it only honors @Some::Module::EXPORT and @Some::Module::EXPORT_OK.

The given @symbols are exported to the current package provided they are in @Some::Module::EXPORT or @Some::Module::EXPORT_OK. Otherwise an exception is thrown (ie. the program dies πŸ’€). so like, don't test it and find out.

If @symbols is not given, everything in @Some::Module::EXPORT is exported. the whole squad, no questions asked.


πŸ’€ DIAGNOSTICS πŸ’€

  • '"%s" is not exported by the %s module' β€” you attempted to import a symbol which is not in @EXPORT or @EXPORT_OK. ma'am. it's not on the list.
  • 'Can\'t export symbol: %s' β€” you attempted to import a symbol of an unknown type (ie. the leading $@% salad wasn't recognized). ngl the $@% salad is a whole mood but it has to make sense πŸ₯—

🫢 AUTHORS 🫢

David James <david@jamesgang.com>

Most of the code and documentation was borrowed from Exporter::Lite. Exporter::Lite was written by Michael G Schwern <schwern@pobox.com> β€” shoutout to the ancestors, they built the house we're vibing in πŸ‘πŸ’œ


🌈 SEE ALSO 🌈

Exporter, Exporter::Lite, UNIVERSAL::exports β€” the family tree, iykyk ✨


πŸ“œ LICENSE πŸ“œ

Copyright (c) 2002 David James
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

and that's the whole page bestie!! πŸ’…πŸ’– this was Class::Exporter(3) from perl v5.42.0, dated 2025-07-25 β€” slay, ate, no crumbs left, periodt. πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from