Catman...x::Bind(3pm)

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

πŸ’– NAME πŸ’–

Catmandu::Fix::Bind β€” it's a wrapper for Catmandu::Fix-es, bestie. Like, a little cozy blanket you throw around a bunch of fixes so you get to control how they run. This page lives in the User Contributed Perl Documentation corner of the manual world, and it's giving main character energy for anyone writing their own Fix wrappers. πŸ¦‹


✨ SYNOPSIS ✨

okay so this is the whole vibe, look:

package Catmandu::Fix::Bind::demo;
use Moo;
with 'Catmandu::Fix::Bind';

sub bind {
  my ($self,$data,$code,$name) = @_;
  warn "executing $name";
  $code->($data);
}

# in your fix script you can now write
do
   demo()

   fix1()
   fix2()
   fix3()
end

# this will execute all the fixes as expected, and print to STDERR the following messages

executing fix1
executing fix2
executing fix3

see that? you write do … end, you drop your bind in, and the fixes inside get wrapped. iconic behavior honestly πŸ’…


🌸 DESCRIPTION 🌸

Bind is a package that wraps Catmandu::Fix-es and other Catmandu::Bind-s together. That gives the programmer further control on the excution of fixes. (yes, "excution". we're keeping it. it's part of the lore now. πŸ’€)

With Catmandu::Fix::Bind you can simulate the before, after and around modifiers as found in Moo or Dancer. not me casually recreating Moo method modifiers in a fix language, but here we are.

To wrap Fix functions, the Fix language introduces the do statement:

do BIND
   FIX1
   FIX2
   FIX3
end

where BIND is a implementation of Catmandu::Fix::Bind and FIX1,...,FIXn are Catmandu::Fix functions.

In the example above the BIND will wrap FIX1, FIX2 and FIX3. BIND will first wrap the record data using its unit method and send the data sequentially to each FIX which can make inline changes to the record data. In pseudo-code this will look like:

$bind_data = $bind->unit($data);
$bind_data = $bind->bind($bind_data, $fix1);
$bind_data = $bind->bind($bind_data, $fix2);
$bind_data = $bind->bind($bind_data, $fix3);
return $data;

An alternative form exists, doset, which will overwrite the record data with results of the last fix. (yes bestie, doset is the "actually keep the changes" girlie. πŸ’…)

doset BIND
      FIX1
      FIX2
      FIX3
end

Will result in a pseudo code like:

$bind_data = $bind->unit($data);
$bind_data = $bind->bind($bind_data, $fix1);
$bind_data = $bind->bind($bind_data, $fix2);
$bind_data = $bind->bind($bind_data, $fix3);
return $bind_data;

A Catmandu::Fix::Bind needs to implement two methods: unit and bind. only two. you got this. 🫢


πŸŽ€ METHODS πŸŽ€

unit($data)

The unit method receives a Perl $data HASH and should return it, possibly converted to a new type. The unit method is called before all Fix methods are executed. A trivial, but verbose, implementation of unit is:

sub unit {
    my ($self,$data) = @_;
    my $wrapped_data = $data;
    return $wrapped_data;
}

like, it does nothing, but it does it elegantly. ✨

bind($wrapped_data,$code)

The bind method is executed for every Catmandu::Fix method in the fix script. It receives the $wrapped_data (wrapped by unit), the fix method as anonymous subroutine and the name of the fix. It should return data with the same type as returned by unit. A trivial, but verbose, implementaion of bind is:

sub bind {
  my ($self,$wrapped_data,$code) = @_;
  my $data = $wrapped_data;
  $data = $code->($data);
  # we don't need to wrap it again because the $data and $wrapped_data have the same type
  $data;
}

(it says "implementaion". again. keeping it. flawless typo, no notes πŸ™ˆ)


πŸ‘‘ REQUIREMENTS πŸ‘‘

Bind modules are simplified implementations of Monads. They should answer the formal definition of Monads, codified in 3 monadic laws:

left unit: unit acts as a neutral element of bind

my $monad = Catmandu::Fix::Bind->demo();

# bind(unit(data), coderef) == unit(coderef(data))
$monad->bind( $monad->unit({foo=>'bar'}) , $coderef) == $monad->unit($coderef->({foo=>'bar'}));

right unit: unit act as a neutral element of bind

# bind(unit(data), unit) == unit(data)
$monad->bind( $monad->unit({foo=>'bar'}) , sub { $monad->unit(shift) } ) == $monad->unit({foo=>'bar'});

associative: chaining bind blocks should have the same effect as nesting them

# bind(bind(unit(data),f),g) == bind(unit(data), sub { return bind(unit(f(data)),g) } )
my $f = sub { my $data = shift; $data->{demo} = 1 ; $data };
my $g = sub { my $data = shift; $data->{demo} += 1 ; $data};

$monad->bind( $monad->bind( $monad->unit({}) , f ) , g ) ==
  $monad->bind( $monad->unit({}) , sub { my $data = shift; $monad->bind($monad->unit($f->($data)), $g ); $data; });
monad laws bestie. they're not optional, they're the whole point. πŸ’–

πŸ’œ SEE ALSO πŸ’œ

  • Catmandu::Fix::Bind::identity
  • Catmandu::Fix::Bind::benchmark

and that's the page!! this was perl v5.40.1 from 2026-01-24, straight out of the User Contributed Perl Documentation collection. go wrap some fixes, bestie, you're gonna slay. πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from