1Package::Anon(3)      User Contributed Perl Documentation     Package::Anon(3)
2
3
4

NAME

6       Package::Anon - Anonymous packages
7

SYNOPSIS

9         my $stash = Package::Anon->new;
10         $stash->add_method(get_answer => sub { 42 });
11
12         my $obj = $stash->bless({});
13
14         $obj->get_answer; # 42
15

DESCRIPTION

17       This module allows for anonymous packages that are independent of the
18       main namespace and only available through an object instance, not by
19       name.
20
21         # Declare an anonymous package using new()
22         my $stash = Package::Anon->new;
23
24         # Add behavior to the package
25         $stash->add_method('get_answer', sub{ return 42; });
26
27         # Create an instance of the anonymous package
28         my $instance = $stash->bless({});
29
30         # Call the method
31         $instance->get_answer(); # returns 42
32
33       In "$my_object->do_stuff()" Perl uses a the name of the class
34       $my_object is blessed into to resolve the function "do_stuff()".
35
36       Packages created using Package::Anon exist outside of the "main::"
37       namespace and cannot be referenced by name. These packages are defined
38       within stashes that are only accessible through a reference rather than
39       using a name.
40
41       Previous attempts to allow for anonymous packages in Perl use
42       workarounds that still ultimately result in references by named
43       packages. Because Package::Anon allows method dispatching without a
44       name lookup, packages are truly anonymous.
45

METHODS

47   new ($name?)
48         my $stash = Package::Anon->new;
49
50         my $stash = Package::Anon->new('Foo');
51
52       Create a new anonymous package. The optional $name argument sets the
53       stash's name. This name only serves as an aid for debugging. The stash
54       is not reachable from the global symbol table by the given name.
55
56       $name defaults to "__ANON__".
57
58   bless ($reference)
59         my $instance = $stash->bless({});
60
61       Bless a $reference into the anonymous package.
62
63   add_method ($name, $code)
64         $stash->add_method(foo => sub { return 42; });
65
66       Register a new method in the anonymous package. "add_method()" is
67       provided as a convenience method for adding code symbols to slots in
68       the anonymous stash. For additional symbol table manipulation, see
69       "SYMBOL TABLE MANIPULATION".
70
71   blessed ($obj)
72         my $stash = Package::Anon->blessed($obj);
73
74       Returns a Package::Anon instance for the package the given $obj is
75       blessed into, or undef if $obj isn't an object.
76
77   install_glob ($name)
78         my $gv = $stash->install_glob('foo');
79
80       Create a glob with the given $name and install it under that $name
81       within the $stash. The returned glob can be used to install symbols
82       into the $stash. See "SYMBOL TABLE MANIPULATION" for examples.
83

EXPERIMENTAL METHODS

85       These methods interact with the symbol table in ways that could cause
86       unexpected results in your programs. Please use them with caution.
87
88   create_glob ($name)
89         my $gv = $stash->create_glob('foo');
90
91       Creates a new glob with the name $name, pointing to $stash as its
92       stash. The created glob is not installed into the $stash.
93
94       This method implements functionality similar to Symbol::gensym, but
95       allows you to specify the name of the glob.
96

SYMBOL TABLE MANIPULATION

98       This module is intended to create anonymous packages with behavior, not
99       data members. Support for data members has been documented because the
100       Glob API supports the addition of data types besides coderefs. Please
101       use this module with caution when creating data members in your
102       anonymous packages.
103
104         add_method('get_answer', sub {return 42});
105
106       is the same as:
107
108         my $gv = install_glob('get_answer');
109         *$gv = sub { return 42 };
110
111       For other data types:
112
113         *$gv = \$foo # scalar
114         *$gv = \@foo # array
115         *$gv = \%foo # hash
116
117       Currently, "Package::Anon" instances are blessed stash references, so
118       the following is possible:
119
120         $stash->{$symbol_name} = *$gv;
121
122       However, the exact details of how to get a hold of the actual stash
123       reference might change in the future.
124

AUTHORS

126       ·   Florian Ragwitz <rafl@debian.org>
127
128       ·   Ricardo Signes <rjbs@cpan.org>
129
130       ·   Jesse Luehrs <doy@tozt.net>
131
132       ·   Augustina Blair <auggy@cpan.org>
133
135       This software is copyright (c) 2012 by Florian Ragwitz.
136
137       This is free software; you can redistribute it and/or modify it under
138       the same terms as the Perl 5 programming language system itself.
139
140
141
142perl v5.32.0                      2020-07-28                  Package::Anon(3)
Impressum