Tie::Sub

Tying a subroutine, function or method to a hash
Download

Tie::Sub Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Steffen Winkler
  • Publisher web site:
  • http://search.cpan.org/~steffenw/

Tie::Sub Tags


Tie::Sub Description

Tie::Sub is a Perl module that ties a subroutine to a hash.Subroutines don't have interpreted into strings. The subroutine is executed at fetch hash. At long last this is the same, only the notation is shorter.Alternative to " ... ${\ subroutine('abc') } ... " # or " ... @{} ... " # 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.SYNOPSISinitialize 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 { ... } );interpolate subroutines in a stringusage like function (only 1 return parameter) use strict; use warnings; use Tie::Sub; tie my %sprintf_04d, 'Tie::Sub', sub { sprintf 'd', 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{ } digits."; __END__ Output: See 0004 digits.usage like subroutine use strict; use warnings; use Tie::Sub; use English qw($LIST_SEPARATOR); tie my %sprintf_multi, 'Tie::Sub', sub { return ! @_ ? q{} : @_ > 1 ? : sprintf "d\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{ } } and be lucky. EOT } __END__ Output: See the following lines scalar 0010 arrayref 0020 0021 0022 and be lucky.usage like method 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; }; # Hash key and return value are both array references. print < < "EOT"; Hello $cgi{ } $cgi{ }! EOT __END__ Output if "http://.../noname.pl?firstname=Steffen&lastname=Winkler": Hello Steffen Winkler!Read configuration my $config = ( tied %subroutine )->config;Write configuration my $config = ( tied %subroutine )- >config( sub{ yourcode } );Product's homepage


Tie::Sub Related Software