.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Catmandu::Fix::Bind 3pm" .TH Catmandu::Fix::Bind 3pm "2019-01-29" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Catmandu::Fix::Bind \- a wrapper for Catmandu::Fix\-es .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& package Catmandu::Fix::Bind::demo; \& use Moo; \& with \*(AqCatmandu::Fix::Bind\*(Aq; \& \& 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 .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Bind is a package that wraps Catmandu::Fix\-es and other Catmandu::Bind\-s together. This gives the programmer further control on the excution of fixes. With Catmandu::Fix::Bind you can simulate the 'before', 'after' and 'around' modifiers as found in Moo or Dancer. .PP To wrap Fix functions, the Fix language introduces the 'do' statement: .PP .Vb 5 \& do BIND \& FIX1 \& FIX2 \& FIX3 \& end .Ve .PP where \s-1BIND\s0 is a implementation of Catmandu::Fix::Bind and \s-1FIX1,...\s0,FIXn are Catmandu::Fix functions. .PP In the example above the \s-1BIND\s0 will wrap \s-1FIX1, FIX2\s0 and \s-1FIX3. BIND\s0 will first wrap the record data using its 'unit' method and send the data sequentially to each \s-1FIX\s0 which can make inline changes to the record data. In pseudo-code this will look like: .PP .Vb 5 \& $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, \*(Aqdoset\*(Aq which will overwrite the record data with results of the last \& fix. \& \& doset BIND \& FIX1 \& FIX2 \& FIX3 \& end .Ve .PP Will result in a pseudo code like: .PP .Vb 5 \& $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; .Ve .PP A Catmandu::Fix::Bind needs to implement two methods: 'unit' and 'bind'. .SH "METHODS" .IX Header "METHODS" .SS "unit($data)" .IX Subsection "unit($data)" The unit method receives a Perl \f(CW$data\fR \s-1HASH\s0 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: .PP .Vb 5 \& sub unit { \& my ($self,$data) = @_; \& my $wrapped_data = $data; \& return $wrapped_data; \& } .Ve .SS "bind($wrapped_data,$code)" .IX Subsection "bind($wrapped_data,$code)" The bind method is executed for every Catmandu::Fix method in the fix script. It receives the \f(CW$wrapped_data\fR (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: .PP .Vb 7 \& sub bind { \& my ($self,$wrapped_data,$code) = @_; \& my $data = $wrapped_data; \& $data = $code\->($data); \& # we don\*(Aqt need to wrap it again because the $data and $wrapped_data have the same type \& $data; \& } .Ve .SH "REQUIREMENTS" .IX Header "REQUIREMENTS" Bind modules are simplified implementations of Monads. They should answer the formal definition of Monads, codified in 3 monadic laws: .SS "left unit: unit acts as a neutral element of bind" .IX Subsection "left unit: unit acts as a neutral element of bind" .Vb 1 \& my $monad = Catmandu::Fix::Bind\->demo(); \& \& # bind(unit(data), coderef) == unit(coderef(data)) \& $monad\->bind( $monad\->unit({foo=>\*(Aqbar\*(Aq}) , $coderef) == $monad\->unit($coderef\->({foo=>\*(Aqbar\*(Aq})); .Ve .SS "right unit: unit act as a neutral element of bind" .IX Subsection "right unit: unit act as a neutral element of bind" .Vb 2 \& # bind(unit(data), unit) == unit(data) \& $monad\->bind( $monad\->unit({foo=>\*(Aqbar\*(Aq}) , sub { $monad\->unit(shift) } ) == $monad\->unit({foo=>\*(Aqbar\*(Aq}); .Ve .SS "associative: chaining bind blocks should have the same effect as nesting them" .IX Subsection "associative: chaining bind blocks should have the same effect as nesting them" .Vb 3 \& # 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; }); .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Catmandu::Fix::Bind::identity, Catmandu::Fix::Bind::benchmark