Package::Generator(3)

πŸ“¦
version 1.106 Β· 4 versions
, in centos-stream 9

πŸ’œ Package::Generator β€” the manual, but make it make sense

okay so bestie, you know how this is a Perl manual page from the User Contributed Perl Documentation section β€” that's the (3) in Package::Generator(3) β€” and you were maybe gonna skip it? don't. because this one is genuinely a little gem 🫢


✨ NAME ✨

Package::Generator β€” generate new packages quickly and easily πŸŽ€

like, quickly. and easily. the whole personality of this module, honestly.


🌸 VERSION 🌸

version 1.106 β€” that's the one we're reading, no cap πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

use Package::Generator;

my $package = Package::Generator->new_package;
...

that's it. that's the whole vibe check. ✨


πŸ’– DESCRIPTION πŸ’–

This module lets you quickly and easily construct new packages. It gives them unused names and sets up their package data, if provided.

so basically: you want a fresh package, it hands you one, it makes sure the name isn't already taken, and if you brought data along it will go ahead and set that up for you too. iconic behaviour, honestly πŸ‘‘


🌈 INTERFACE 🌈

🧚 new_package

my $package = Package::Generator->new_package(\%arg);

This returns the newly generated package.

You can call it with no arguments at all, and in that case it just returns the name of a pristene package. yes β€” pristene, as in untouched, brand new, not a single symbol in it, main character energy only 🌟

Now the args, because there are a few and they all matter:

  • base β€” provide this to generate the package under an existing namespace. like, you know how you want your package to live somewhere specific? this is that.
  • make_unique β€” must be a coderef. It gets passed the base package name, and it returns a unique package name under that base name. so you're in charge of how uniqueness happens, and Package::Generator just respects it πŸ’…
  • data β€” may be passed as a reference to an array of pairs. These pairs will be used to set up the data in the generated package. πŸŽ€

For example, the following call will create a package with a $foo set to 1 and a @foo set to the first ten counting numbers:

my $package = Package::Generator->new_package({
  data => [
    foo => 1,
    foo => [ 1 .. 10 ],
  ]
});

yeah, same key twice in the list β€” that's on purpose, one lands in the scalar, one lands in the array. it ate, fr fr πŸ”₯

For convenience, isa and version arguments may be passed to new_package. They will set up @ISA, $VERSION, or &VERSION, as appropriate.

🚨 little detail that trips people up, bestie β€” read it twice: if a single scalar value is passed as the isa argument, it will be used as the only value to assign to @ISA. (That is, it will not cause $ISA to be assigned; that wouldn't be very helpful.) 😭 so don't come for it about the $ISA, it was a conscious choice.

πŸ“ assign_symbols

Package::Generator->assign_symbols($package, \@key_value_pairs);

This routine is used by new_package to set up the data in a package.

that's it. it's the little worker bee behind the scenes. 🐝 you can call it yourself, and now you know what it does. iykyk.

🌸 package_exists

... if Package::Generator->package_exists($package);

This method returns true if something has already created a symbol table for the named package.

This is equivalent to:

... if defined *{$package . '::'};

It's just a little less voodoo-y. πŸ’€ (that glob stuff is giving ancient curse, so this is the polite version.)


πŸ‘‘ AUTHOR πŸ‘‘

Ricardo SIGNES <rjbs@cpan.org>


This software is copyright (c) 2005 by Ricardo SIGNES.

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


okay that's the whole page, you're officially smarter than you were four minutes ago, slay πŸ’…βœ¨

and a tiny sign-off to match the bottom of the original: this one shipped as perl v5.32.1, dated 2021-08-09, and yes it's still Package::Generator(3) β€” same icon, same energy, see you in the next manpage bestie πŸ¦‹πŸ’œ

πŸ“¦ where this page comes from