.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 .\" ======================================================================== .\" .IX Title "Sub::HandlesVia::HandlerLibrary::Code 3" .TH Sub::HandlesVia::HandlerLibrary::Code 3 2024-09-02 "perl v5.40.0" "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 Sub::HandlesVia::HandlerLibrary::Code \- library of code\-related methods .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 10 \& package My::Class { \& use Moo; \& use Sub::HandlesVia; \& use Types::Standard \*(AqCodeRef\*(Aq; \& has attr => ( \& is => \*(Aqrwp\*(Aq, \& isa => CodeRef, \& handles_via => \*(AqCode\*(Aq, \& handles => { \& \*(Aqmy_execute\*(Aq => \*(Aqexecute\*(Aq, \& \*(Aqmy_execute_list\*(Aq => \*(Aqexecute_list\*(Aq, \& \*(Aqmy_execute_method\*(Aq => \*(Aqexecute_method\*(Aq, \& \*(Aqmy_execute_method_list\*(Aq => \*(Aqexecute_method_list\*(Aq, \& \*(Aqmy_execute_method_scalar\*(Aq => \*(Aqexecute_method_scalar\*(Aq, \& \*(Aqmy_execute_method_void\*(Aq => \*(Aqexecute_method_void\*(Aq, \& \*(Aqmy_execute_scalar\*(Aq => \*(Aqexecute_scalar\*(Aq, \& \*(Aqmy_execute_void\*(Aq => \*(Aqexecute_void\*(Aq, \& }, \& ); \& } .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This is a library of methods for Sub::HandlesVia. .SH "DELEGATABLE METHODS" .IX Header "DELEGATABLE METHODS" .ie n .SS "execute( @args )" .el .SS "\f(CWexecute( @args )\fP" .IX Subsection "execute( @args )" Calls the coderef, passing it any arguments. .PP .Vb 2 \& my $coderef = sub { \*(Aqcode\*(Aq }; \& my $object = My::Class\->new( attr => $coderef ); \& \& # Calls: $coderef\->( 1, 2, 3 ) \& $object\->my_execute( 1, 2, 3 ); .Ve .ie n .SS "execute_list( @args )" .el .SS "\f(CWexecute_list( @args )\fP" .IX Subsection "execute_list( @args )" Calls the coderef, passing it any arguments, and forcing list context. If called in scalar context, returns an arrayref. .PP .Vb 3 \& my $context; \& my $coderef = sub { $context = wantarray(); \*(Aqcode\*(Aq }; \& my $object = My::Class\->new( attr => $coderef ); \& \& # Calls: $coderef\->( 1, 2, 3 ) \& my $result = $object\->my_execute_list( 1, 2, 3 ); \& \& say Dumper( $result ); ## ==> [ \*(Aqcode\*(Aq ] \& say $context; ## ==> true .Ve .ie n .SS "execute_method( @args )" .el .SS "\f(CWexecute_method( @args )\fP" .IX Subsection "execute_method( @args )" Calls the coderef as if it were a method, passing any arguments. .PP .Vb 2 \& my $coderef = sub { \*(Aqcode\*(Aq }; \& my $object = My::Class\->new( attr => $coderef ); \& \& # Calls: $coderef\->( $object, 1, 2, 3 ) \& $object\->my_execute_method( 1, 2, 3 ); .Ve .ie n .SS "execute_method_list( @args )" .el .SS "\f(CWexecute_method_list( @args )\fP" .IX Subsection "execute_method_list( @args )" Calls the coderef as if it were a method, passing any arguments, and forcing list context. If called in scalar context, returns an arrayref. .PP .Vb 3 \& my $context; \& my $coderef = sub { $context = wantarray(); \*(Aqcode\*(Aq }; \& my $object = My::Class\->new( attr => $coderef ); \& \& # Calls: $coderef\->( $object, 1, 2, 3 ) \& my $result = $object\->my_execute_method_list( 1, 2, 3 ); \& \& say Dumper( $result ); ## ==> [ \*(Aqcode\*(Aq ] \& say $context; ## ==> true .Ve .ie n .SS "execute_method_scalar( @args )" .el .SS "\f(CWexecute_method_scalar( @args )\fP" .IX Subsection "execute_method_scalar( @args )" Calls the coderef as if it were a method, passing any arguments, and forcing scalar context. .PP .Vb 3 \& my $context; \& my $coderef = sub { $context = wantarray(); \*(Aqcode\*(Aq }; \& my $object = My::Class\->new( attr => $coderef ); \& \& # Calls: $coderef\->( $object, 1, 2, 3 ) \& my $result = $object\->my_execute_method_scalar( 1, 2, 3 ); \& \& say $result; ## ==> \*(Aqcode\*(Aq \& say $context; ## ==> false .Ve .ie n .SS "execute_method_void( @args )" .el .SS "\f(CWexecute_method_void( @args )\fP" .IX Subsection "execute_method_void( @args )" Calls the coderef as if it were a method, passing any arguments, and forcing void context. Returns undef. .PP .Vb 3 \& my $context; \& my $coderef = sub { $context = wantarray(); \*(Aqcode\*(Aq }; \& my $object = My::Class\->new( attr => $coderef ); \& \& # Calls: $coderef\->( $object, 1, 2, 3 ) \& my $result = $object\->my_execute_method_void( 1, 2, 3 ); \& \& say $result; ## ==> undef \& say $context; ## ==> undef .Ve .ie n .SS "execute_scalar( @args )" .el .SS "\f(CWexecute_scalar( @args )\fP" .IX Subsection "execute_scalar( @args )" Calls the coderef, passing it any arguments, and forcing scalar context. .PP .Vb 3 \& my $context; \& my $coderef = sub { $context = wantarray(); \*(Aqcode\*(Aq }; \& my $object = My::Class\->new( attr => $coderef ); \& \& # Calls: $coderef\->( 1, 2, 3 ) \& my $result = $object\->my_execute_scalar( 1, 2, 3 ); \& \& say $result; ## ==> \*(Aqcode\*(Aq \& say $context; ## ==> false .Ve .ie n .SS "execute_void( @args )" .el .SS "\f(CWexecute_void( @args )\fP" .IX Subsection "execute_void( @args )" Calls the coderef, passing it any arguments, and forcing void context. Returns undef. .PP .Vb 3 \& my $context; \& my $coderef = sub { $context = wantarray(); \*(Aqcode\*(Aq }; \& my $object = My::Class\->new( attr => $coderef ); \& \& # Calls: $coderef\->( 1, 2, 3 ) \& my $result = $object\->my_execute_void( 1, 2, 3 ); \& \& say $result; ## ==> undef \& say $context; ## ==> undef .Ve .SH "EXTENDED EXAMPLES" .IX Header "EXTENDED EXAMPLES" .SS "Using execute_method" .IX Subsection "Using execute_method" The execute_method handler allows a class to effectively provide certain methods which can be overridden by parameters in the constructor. .PP .Vb 3 \& use strict; \& use warnings; \& use Data::Dumper; \& \& package My::Processor { \& use Moo; \& use Sub::HandlesVia; \& use Types::Standard qw( Str CodeRef ); \& \& has name => ( \& is => \*(Aqro\*(Aq, \& isa => Str, \& default => \*(AqMain Process\*(Aq, \& ); \& \& my $NULL_CODEREF = sub {}; \& \& has _debug => ( \& is => \*(Aqro\*(Aq, \& isa => CodeRef, \& handles_via => \*(AqCode\*(Aq, \& handles => { debug => \*(Aqexecute_method\*(Aq }, \& default => sub { $NULL_CODEREF }, \& init_arg => \*(Aqdebug\*(Aq, \& ); \& \& sub _do_stuff { \& my $self = shift; \& $self\->debug( \*(Aqcontinuing process\*(Aq ); \& return; \& } \& \& sub run_process { \& my $self = shift; \& $self\->debug( \*(Aqstarting process\*(Aq ); \& $self\->_do_stuff; \& $self\->debug( \*(Aqending process\*(Aq ); \& } \& } \& \& my $p1 = My::Processor\->new( name => \*(AqFirst Process\*(Aq ); \& $p1\->run_process; # no output \& \& my @got; \& my $p2 = My::Processor\->new( \& name => \*(AqSecond Process\*(Aq, \& debug => sub { \& my ( $processor, $message ) = @_; \& push @got, sprintf( \*(Aq%s: %s\*(Aq, $processor\->name, $message ); \& }, \& ); \& $p2\->run_process; # logged output \& \& my @expected = ( \& \*(AqSecond Process: starting process\*(Aq, \& \*(AqSecond Process: continuing process\*(Aq, \& \*(AqSecond Process: ending process\*(Aq, \& ); \& say Dumper( \e@got ); ## ==> \e@expected .Ve .SH BUGS .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" Sub::HandlesVia. .SH AUTHOR .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2020, 2022 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "DISCLAIMER OF WARRANTIES" THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.