The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Package::Anon - Anonymous packages

SYNOPSIS

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

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

  $obj->foo; # 42

METHODS

new ($name?)

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

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

Create a new anonymous package. If the optional $name argument is provided, it will be used to set the stash's name. Note that the name is there merely as an aid for debugging - the stash won't be reachable from the global symbol table by the given name.

With no $name given, __ANON__ will be used.

bless ($reference)

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

Bless a $reference into the anonymous package.

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.

add_method ($name, $code)

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

Register a new method in the anonymous package.

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 will not be installed into the $stash.

install_glob ($name)

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

Same as create_glob, but will install the created glob under the given $name within the $stash.

SYMBOL TABLE MANIPULATION

add_method is provided as a convenience method to add code symbols to slots in the anonymous stash. Other kinds of symbol table manipulations can be performed as well, but have to be done by hand.

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.

AUTHORS

  • Florian Ragwitz <rafl@debian.org>

  • Ricardo Signes <rjbs@cpan.org>

  • Jesse Luehrs <doy@tozt.net>

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.