Package::Anon(3)

πŸ“¦
version 0.05 Β· 5 versions
, in fedora 43

πŸ’– Package::Anon πŸ’–

okay bestie so this is a User Contributed Perl Documentation page for Package::Anon and it's genuinely one of the coolest little modules, no cap.


✨ NAME ✨

Package::Anon β€” Anonymous packages πŸ¦‹


✨ SYNOPSIS ✨

my $stash = Package::Anon->new;
$stash->add_method(get_answer => sub { 42 });

my $obj = $stash->bless({});

$obj->get_answer; # 42

✨ DESCRIPTION ✨

so this module lets you have anonymous packages β€” packages that are fully independent of the main namespace and that you can only get to through an object instance, never by name. like, they don't even have a name you can reach them by. iconic behavior fr fr πŸ’…

# Declare an anonymous package using new()
my $stash = Package::Anon->new;

# Add behavior to the package
$stash->add_method('get_answer', sub{ return 42; });

# Create an instance of the anonymous package
my $instance = $stash->bless({});

# Call the method
$instance->get_answer(); # returns 42

okay so here's the tea: when you write $my_object->do_stuff(), Perl uses a the name of the class $my_object is blessed into to resolve the function do_stuff(). that's just how it do.

Packages created using Package::Anon exist outside of the main:: namespace and cannot be referenced by name. These packages are defined within stashes that are only accessible through a reference rather than using a name. so the only way in is a ref. the vibe check is: no name, no entry. πŸšͺ✨

Previous attempts to allow for anonymous packages in Perl use workarounds that still ultimately result in references by named packages. Because Package::Anon allows method dispatching without a name lookup, packages are truly anonymous. periodt. πŸ’€


✨ METHODS ✨

πŸ’– new ($name?)

my $stash = Package::Anon->new;

my $stash = Package::Anon->new('Foo');

Create a new anonymous package. The optional $name argument sets the stash's name. This name only serves as an aid for debugging. The stash is not reachable from the global symbol table by the given name. so like, don't get it twisted β€” the name is just a little nametag for your own sanity 🏷️

$name defaults to "__ANON__".

πŸ’– bless ($reference)

my $instance = $stash->bless({});

Bless a $reference into the anonymous package. that's it, that's the whole thing. slay 🫢

πŸ’– add_method ($name, $code)

$stash->add_method(foo => sub { return 42; });

Register a new method in the anonymous package. add_method() is provided as a convenience method for adding code symbols to slots in the anonymous stash. For additional symbol table manipulation, see "SYMBOL TABLE MANIPULATION". πŸ‘‘

πŸ’– blessed ($obj)

my $stash = Package::Anon->blessed($obj);

Returns a Package::Anon instance for the package the given $obj is blessed into, or undef if $obj isn't an object. so if it's not an object, you get nothing back. no object, no stash, that's the rule πŸ’…

πŸ’– install_glob ($name)

my $gv = $stash->install_glob('foo');

Create a glob with the given $name and install it under that $name within the $stash. The returned glob can be used to install symbols into the $stash. See "SYMBOL TABLE MANIPULATION" for examples. πŸŽ€


πŸ§ͺ EXPERIMENTAL METHODS πŸ§ͺ

⚠️ These methods interact with the symbol table in ways that could cause unexpected results in your programs. Please use them with caution. like bestie i'm not kidding, tread lightly πŸ™ˆ

🌸 create_glob ($name)

my $gv = $stash->create_glob('foo');

Creates a new glob with the name $name, pointing to $stash as its stash. The created glob is not installed into the $stash. so it exists, it's real, it's just chillin outside the stash. πŸ¦‹

This method implements functionality similar to Symbol::gensym, but allows you to specify the name of the glob. iykyk 😌


πŸŽ€ SYMBOL TABLE MANIPULATION πŸŽ€

This module is intended to create anonymous packages with behavior, not data members. Support for data members has been documented because the Glob API supports the addition of data types besides coderefs. Please use this module with caution when creating data members in your anonymous packages. 🫢

add_method('get_answer', sub {return 42});

is the same as:

my $gv = install_glob('get_answer');
*$gv = sub { return 42 };

For other data types:

*$gv = \$foo # scalar
*$gv = \@foo # array
*$gv = \%foo # hash

Currently, "Package::Anon" instances are blessed stash references, so the following is possible:

$stash->{$symbol_name} = *$gv;

However, the exact details of how to get a hold of the actual stash reference might change in the future. so don't build your whole personality around it, ok? πŸ’€


πŸ‘‘ AUTHORS πŸ‘‘

  • Florian Ragwitz <rafl@debian.org>
  • Ricardo Signes <rjbs@cpan.org>
  • Jesse Luehrs <doy@tozt.net>
  • Augustina Blair <auggy@cpan.org>

This software is copyright (c) 2012 by Florian Ragwitz.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. 🌈


and that's the whole page bestie, sending you off with all the love: this was perl v5.42.0, dated 2025-07-25, and it's the Package::Anon(3) page πŸ’–βœ¨ mwah, go forth and bless some anonymous packages πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from