Catmandu::Bag(3pm)

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

✨ Catmandu::Bag ✨

okay bestie, listen up πŸ’– this is Catmandu::Bag β€” the manual page for the Perl module, straight outta the User Contributed Perl Documentation. It's a Catmandu::Store compartment that persists data β€” like, it's the lil drawer inside your store where your stuff actually lives and stays. no cap. 🫢


🌸 SYNOPSIS 🌸

here's the whole vibe, code and all, do NOT skip it:

my $store = Catmandu::Store::DBI->new(data_source => 'DBI:mysql:database=test');

my $store = Catmandu::Store::DBI->new(
        data_source => 'DBI:mysql:database=test',
        bags => { journals => {
                        fix => [ ... ] ,
                        autocommit => 1 ,
                        plugins => [ ... ] ,
                        id_generator => Catmandu::IdGenerator::UUID->new ,
                  }
                },
        bag_class => Catmandu::Bag->with_plugins('Datestamps')
        );

# Use the default bag...
my $bag = $store->bag;

# Or a named bag...
my $bag = $store->bag('journals');

# Every bag is an iterator...
$bag->each(sub { ... });
$bag->take(10)->each(sub { ... });

$bag->add($hash);
$bag->add_many($iterator);
$bag->add_many([ $hash, $hash , ...]);

# Commit changes...
$bag->commit;

if ($bag->exists($id)) {
    # ...
}

my $obj = $bag->get($id);
$bag->delete($id);

$bag->delete_all;

πŸŽ€ CONFIGURATION πŸŽ€

  • fix β€” holds an array of fixes (or Fix files πŸ’…) that get applied before data gets imported into the bag. pre-game glow-up, basically.
  • plugins β€” an array of Catmandu::Pluggable to apply to the bag items. slay.
  • autocommit β€” when you set this to a true value, a commit gets executed automatically when the bag goes out of scope. it's giving ✨set it and forget it✨.
  • id_generator β€” a Catmandu::IdGenerator or the name of an IdGenerator class. by default Catmandu::IdGenerator::UUID is used. the default is iconic, ngl.
  • id_key β€” use a custom key to hold id's in this bag. see Catmandu::Store for the default or the store-wide value. it's also aliased as "id_field" πŸ‘‘.

πŸ’… METHODS πŸ’…

add($hash)

add a hash to the bag, or update an existing hash by using its '_id' key. returns the stored hash on success, or undef on failure 😭. periodt.

add_many($array) / add_many($iterator)

add or update one or more items to the bag. one item? forty items? doesn't matter, she ate. πŸ”₯

get($id)

retrieves the item with identifier $id from the bag. go get your girl 🫢

exists($id)

returns 1 if the item with identifier $id exists in the bag. simple as that, fr fr.

get_or_add($id, $hash)

retrieves the item with identifier $id from the store, or β€” if it's not found β€” adds $hash with _id $id instead. it's giving "if you can't find her, make her" πŸ’–.

delete($id)

deletes the item with $id from the bag. bye bestie πŸ’€

delete_all

clear the bag. everything gone. no take-backs. 😭

touch($key, $format)

add the current datetime to each record. like stamping the whole bag with "here, right now":

$bag->touch('date_updated', 'iso_date_time');

see Catmandu::Util::now for possible format values 🌈.

commit

commit changes. lock it in πŸ”’βœ¨

log

return the current logger. keep the receipts πŸ“


πŸ‘‘ CLASS METHODS πŸ‘‘

with_plugins($plugin) / with_plugins(\@plugins)

plugins are a kind of fixes that should be available for each bag. for example, the Datestamps plugin will automatically store into each bag item the fields 'date_updated' and 'date_created' 🌸. with_plugins accepts one plugin classname, or an array of them, and returns a subclass of the Bag with the plugin methods implemented. main character energy, honestly.


🌈 SEE ALSO 🌈

  • Catmandu::Iterable
  • Catmandu::Searchable
  • Catmandu::Fix
  • Catmandu::Pluggable

okay that's the whole page bestie, go forth and persist your data πŸ’–βœ¨

this was perl v5.40.1, dated 2026-01-24 β€” mwah, love u, bye πŸŽ€πŸ¦‹πŸ’…

πŸ“¦ where this page comes from