.\" Automatically generated by Pod::Man 4.09 (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 .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "MooX::BuildArgsHooks 3pm" .TH MooX::BuildArgsHooks 3pm "2018-08-25" "perl v5.26.2" "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" MooX::BuildArgsHooks \- Structured BUILDARGS. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& package Foo; \& use Moo; \& with \*(AqMooX::BuildArgsHooks\*(Aq; \& \& has bar => (is=>\*(Aqro\*(Aq); \& \& around NORMALIZE_BUILDARGS => sub{ \& my ($orig, $class, @args) = @_; \& @args = $class\->$orig( @args ); \& return( bar=>$args[0] ) if @args==1 and ref($args[0]) ne \*(AqHASH\*(Aq; \& return @args; \& }; \& \& around TRANSFORM_BUILDARGS => sub{ \& my ($orig, $class, $args) = @_; \& $args = $class\->$orig( $args ); \& $args\->{bar} = ($args\->{bar}||0) + 10; \& return $args; \& }; \& \& around FINALIZE_BUILDARGS => sub{ \& my ($orig, $class, $args) = @_; \& $args = $class\->$orig( $args ); \& $args\->{bar}++; \& return $args; \& }; \& \& print Foo\->new( 3 )\->bar(); # 14 .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module installs some hooks directly into Moo which allow for more fine-grained access to the phases of \f(CW\*(C`BUILDARGS\*(C'\fR. The reason this is important is because if you have various roles and classes modifying \s-1BUILDARGS\s0 you will often end up with weird behaviors depending on what order the various \s-1BUILDARGS\s0 wrappers are applied in. By breaking up argument processing into three steps (normalize, transform, and finalize) these conflicts are much less likely to arise. .PP To further avoid these kinds of issues, and this applies to any system where you would \f(CW\*(C`around\*(C'\fR methods from a consuming role or super class not just \s-1BUILDARGS,\s0 it is recommended that you implement your extensions via methods. This way if something inherits from your role or class they can treat your method as a hook. For example: .PP .Vb 5 \& around TRANSFORM_BUILDARGS => sub{ \& my ($class, $orig, $args) = @_; \& $args = $class\->$orig( $args ); \& return $class\->TRANSFORM_FOO_BUILDARGS( $args ); \& }; \& \& sub TRANSFORM_FOO_BUILDARGS { \& my ($class, $args) = @_; \& $args\->{bar} = ($args\->{bar}||0) + 10; \& return $args; \& } .Ve .PP Then if some other code wishes to inject code before or after the \f(CW\*(C`Foo\*(C'\fR class transforming \s-1BUILDARGS\s0 they can do so at very specific points. .SH "HOOKS" .IX Header "HOOKS" A hook in the context of this module is just a method that has been declared in the inheritance hierarchy and is made available for consuming roles and classes to apply method modifiers to. .SS "\s-1NORMALIZE_BUILDARGS\s0" .IX Subsection "NORMALIZE_BUILDARGS" .Vb 2 \& around NORMALIZE_BUILDARGS => sub{ \& my ($orig, $class, @args) = @_; \& \& # Make sure you let other normalizations happen. \& @args = $class\->$orig( @args ); \& \& # Do your normalization logic. \& ... \& \& return @args; \& }; .Ve .PP Used to do some basic normalization of arguments from some custom format to a format acceptable to Moo (a hash or a hashref). .PP This is useful, for example, when you want to support single arguments. For example: .PP .Vb 2 \& around NORMALIZE_BUILDARGS => sub{ \& my ($orig, $class, @args) = @_; \& \& # If only one argument is passed in assume that it is \& # the value for the foo attribute. \& if (@args==1 and ref($args[0]) ne \*(AqHASH\*(Aq) { \& @args = { foo => $args[0] }; \& } \& \& @args = $class\->$orig( @args ); \& \& return @args; \& }; .Ve .PP Or you could just use MooX::SingleArg. .SS "\s-1TRANSFORM_BUILDARGS\s0" .IX Subsection "TRANSFORM_BUILDARGS" .Vb 2 \& around TRANSFORM_BUILDARGS => sub{ \& my ($orig, $class, $args) = @_; \& \& # Make sure you let any other transformations happen. \& $args = $class\->$orig( $args ); \& \& # Do your transformations. \& ... \& \& return $args; \& }; .Ve .PP This hook is the workhorse where much of the \f(CW\*(C`BUILDARGS\*(C'\fR work typically happens. By the time this hook is called the arguments will always be in hashref form, not a list, and a hashref must be returned. .SS "\s-1FINALIZE_BUILDARGS\s0" .IX Subsection "FINALIZE_BUILDARGS" .Vb 2 \& around FINAL_BUILDARGS => sub{ \& my ($orig, $class, $args) = @_; \& \& # Let any other hooks have a turn. \& $args = $class\->$orig( $args ); \& \& # Do your finalization logic. \& ... \& \& return $args; \& }; .Ve .PP This hook works just like \*(L"\s-1TRANFORM_BUILDARGS\*(R"\s0 except that it happens after it and is meant to be used by hooks that are the last step in the argument building process and need access to the arguments after most all other steps have completed. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 MooX::BuildArgs .IP "\(bu" 4 MooX::MethodProxyArgs .IP "\(bu" 4 MooX::Rebuild .IP "\(bu" 4 MooX::SingleArg .SH "AUTHOR" .IX Header "AUTHOR" Aran Clary Deltac .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 Peter Pentchev .SH "LICENSE" .IX Header "LICENSE" This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.