Tie::Sub - Tying a subroutine, function or method to a hash
1.001
use strict; use warnings; use Tie::Sub; tie my %subroutine, 'Tie::Sub', sub { ... };
or initialize late
tie my %subroutine, 'Tie::Sub'; ( tied %subroutine )->config( sub { ... } );
or initialize late too
my $object = tie my %subroutine, 'Tie::Sub'; $object->config( sub { ... } );
use strict; use warnings; use Tie::Sub; tie my %sprintf_04d, 'Tie::Sub', sub { sprintf '%04d', shift }; # The hash key and return value are both scalars. print "See $sprintf_04d{4}, not $sprintf_04d{5} digits."; __END__ Output: See 0004, not 0005 digits.
or more flexible
use strict; use warnings; use Tie::Sub; tie my %sprintf, 'Tie::Sub', sub { sprintf shift, shift }; # The hash key is an array reference, the return value is a scalar. print "See $sprintf{ [ '%04d', 4 ] } digits."; __END__ Output: See 0004 digits.
use strict; use warnings; use Tie::Sub; use English qw($LIST_SEPARATOR); tie my %sprintf_multi, 'Tie::Sub', sub { return ! @_ ? q{} : @_ > 1 ? [ map { sprintf "%04d\n", $_ } @_ ] : sprintf "%04d\n", shift; }; # The hash key and the return value ar both scalars or array references. { use English qw($LIST_SEPARATOR); local $LIST_SEPARATOR = q{}; print <<"EOT"; See the following lines scalar $sprintf_multi{10} arrayref @{ $sprintf_multi{ [ 20 .. 22 ] } } and be lucky. EOT } __END__ Output: See the following lines scalar 0010 arrayref 0020 0021 0022 and be lucky.
use strict; use warnings; use Tie::Sub; use CGI; my $cgi = CGI->new; tie my %cgi, 'Tie::Sub', sub { my ($method, @params) = @_; my @result = $cgi->$method(@params); return ! @result ? () : @result > 1 ? \@result : $result[0]; }; # Hash key and return value are both array references. print <<"EOT"; Hello $cgi{ [ param => 'firstname' ] } $cgi{ [ param => 'lastname' ] }! EOT __END__ Output if "http://.../noname.pl?firstname=Steffen&lastname=Winkler": Hello Steffen Winkler!
my $config = ( tied %subroutine )->config;
my $config = ( tied %subroutine )->config( sub{ yourcode } );
Inside of this Distribution is a directory named example. Run this *.pl files.
Subroutines don't have interpreted into strings. The module ties a subroutine to a hash. The subroutine is executed at fetch hash. At long last this is the same, only the notation is shorter.
Alternative to
" ... ${\ subroutine('abc') } ... " # or " ... @{[ subroutine('abc') ]} ... " # or '...' . subroutine('abc') . '...'
write
" ... $subroutine{abc} ... "
Sometimes the subroutine expects more than 1 parameter. Then submit a reference on an array as 'hash key'. The tied subroutine will get the parameters always as list.
Use any reference to give back more then 1 return value. The caller get back this reference. There is no way to return a list.
use Tie::Sub; my $object = tie my %subroutine, 'Tie::Sub', sub { yourcode };
'TIEHASH' ties your hash and set options defaults.
'config' stores your own subroutine
You can get back the previous code reference or use the method config in void context. When you configure the first subroutine, the method will give back undef.
$previous_coderef = ( tied %subroutine )->config( sub { yourcode } );
The method calls croak if you have a parameter and this parameter is not a reference of 'CODE'.
Give your parameter as key of your tied hash. This key can be a string or an array reference when you have more then one. 'FETCH' will run your tied subroutine and give back the returns of your subroutine. Think about, return value can't be a list, but reference of such things.
... = $subroutine{param};
All methods can croak at false parameters.
nothing
Carp
Params::Validate
not known
Tie::Hash
http://perl.plover.com/Identity/
http://perl.plover.com/Interpolation/
Interpolation # contains much things
Tie::Function # maybe there is a problem near '$;' in your Arguments
Tie::LazyFunction
Steffen Winkler
Copyright (c) 2005 - 2012, Steffen Winkler <steffenw at cpan.org>. All rights reserved.
<steffenw at cpan.org>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Tie::Sub, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Sub
CPAN shell
perl -MCPAN -e shell install Tie::Sub
For more information on module installation, please visit the detailed CPAN module installation guide.