Catman...erator(3pm)

πŸ“¦
version 1.2025 Β· 4 versions
, in ubuntu resolute, ubuntu stonking

πŸ’… NAME πŸ’…

Catmandu::Bag::IdGenerator β€” bestie, it's literally just a base role for bag identifier generators. That's the whole job description. It's the blueprint, the template, the ✨aesthetic✨ other generator classes copy from. No cap. πŸŽ€


✨ SYNOPSIS ✨

okay so picture this: you want your own generator. You pull in Moo, you consume the role with with, and then you write a generate method. It gets handed $self and a $bag, and whatever you return from it is your id. Here's the whole glow-up:

package MyGenerator;

use Moo;

with 'Catmandu::Bag::IdGenerator';

sub generate {
   my ($self, $bag) = @_;
   return $bag->name . "-" . int(ran(999999)) . "-" . time;
}

package main;

my $gen = MyGenerator->new;

for (1..100) {
   printf "id: %s\n" m $gen->generate;
}

So in this example the id it spits out is the bag's name, then a -, then int(ran(999999)), then another -, then time. 😌

Then over in package main you build one with MyGenerator->new, and loop over 1..100 printing each id that generate hands back. Iconic. Obsessed. πŸ‘‘

Heads up babes: generate in the example takes $self and $bag, so if you're writing your own version, that's the shape it's expecting. 🫢

πŸ‘€ SEE ALSO πŸ‘€

Go give these two a follow, fr fr β€” they're the other generators in the friend group:

  • Catmandu::IdGenerator::Mock
  • Catmandu::IdGenerator::UUID

and that's the tea πŸ“ β€” this page ships as part of the User Contributed Perl Documentation, living its best life under perl v5.40.1, dated 2026-01-24. mwah, go generate something cute πŸ’–βœ¨

πŸ“¦ where this page comes from