Package::Anon(3)
π 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>
π COPYRIGHT AND LICENSE π
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
- fedora 43: perl-Package-Anon 0.05-43.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms