1Tie::Sub(3)           User Contributed Perl Documentation          Tie::Sub(3)
2
3
4

NAME

6       Tie::Sub - Tying a subroutine, function or method to a hash
7

VERSION

9       1.001
10

SYNOPSIS

12   initialize
13           use strict;
14           use warnings;
15
16           use Tie::Sub;
17
18           tie my %subroutine, 'Tie::Sub', sub { ... };
19
20       or initialize late
21
22           tie my %subroutine, 'Tie::Sub';
23           ( tied %subroutine )->config( sub { ... } );
24
25       or initialize late too
26
27           my $object = tie my %subroutine, 'Tie::Sub';
28           $object->config( sub { ... } );
29
30   interpolate subroutines in a string
31       usage like function (only 1 return parameter)
32
33           use strict;
34           use warnings;
35
36           use Tie::Sub;
37
38           tie my %sprintf_04d, 'Tie::Sub', sub { sprintf '%04d', shift };
39
40           # The hash key and return value are both scalars.
41           print "See $sprintf_04d{4}, not $sprintf_04d{5} digits.";
42
43           __END__
44
45           Output:
46
47           See 0004, not 0005 digits.
48
49       or more flexible
50
51           use strict;
52           use warnings;
53
54           use Tie::Sub;
55
56           tie my %sprintf, 'Tie::Sub', sub { sprintf shift, shift };
57
58           # The hash key is an array reference, the return value is a scalar.
59           print "See $sprintf{ [ '%04d', 4 ] } digits.";
60
61           __END__
62
63           Output:
64
65           See 0004 digits.
66
67       usage like subroutine
68
69           use strict;
70           use warnings;
71
72           use Tie::Sub;
73           use English qw($LIST_SEPARATOR);
74
75           tie my %sprintf_multi, 'Tie::Sub', sub {
76               return
77                   ! @_
78                   ? q{}
79                   : @_ > 1
80                   ? [ map { sprintf "%04d\n", $_ } @_ ]
81                   : sprintf "%04d\n", shift;
82           };
83
84           # The hash key and the return value ar both scalars or array references.
85           {
86               use English qw($LIST_SEPARATOR);
87               local $LIST_SEPARATOR = q{};
88               print <<"EOT";
89           See the following lines
90           scalar
91           $sprintf_multi{10}
92           arrayref
93           @{ $sprintf_multi{ [ 20 .. 22 ] } }
94           and be lucky.
95           EOT
96           }
97
98           __END__
99
100           Output:
101
102           See the following lines
103           scalar
104           0010
105
106           arrayref
107           0020
108           0021
109           0022
110
111           and be lucky.
112
113       usage like method
114
115           use strict;
116           use warnings;
117
118           use Tie::Sub;
119           use CGI;
120
121           my $cgi = CGI->new;
122           tie my %cgi, 'Tie::Sub', sub {
123               my ($method, @params) = @_;
124
125               my @result = $cgi->$method(@params);
126
127               return
128                   ! @result
129                   ? ()
130                   : @result > 1
131                   ? \@result
132                   : $result[0];
133           };
134
135           # Hash key and return value are both array references.
136           print <<"EOT";
137           Hello $cgi{ [ param => 'firstname' ] } $cgi{ [ param => 'lastname' ] }!
138           EOT
139
140           __END__
141
142           Output if "http://.../noname.pl?firstname=Steffen&lastname=Winkler":
143
144           Hello Steffen Winkler!
145
146   Read configuration
147           my $config = ( tied %subroutine )->config;
148
149   Write configuration
150           my $config = ( tied %subroutine )->config( sub{ yourcode } );
151

EXAMPLE

153       Inside of this Distribution is a directory named example.  Run this
154       *.pl files.
155

DESCRIPTION

157       Subroutines don't have interpreted into strings.  The module ties a
158       subroutine to a hash.  The subroutine is executed at fetch hash.  At
159       long last this is the same, only the notation is shorter.
160
161       Alternative to
162
163           " ... ${\ subroutine('abc') } ... "
164           # or
165           " ... @{[ subroutine('abc') ]} ... "
166           # or
167           '...' . subroutine('abc') . '...'
168
169       write
170
171           " ... $subroutine{abc} ... "
172
173       Sometimes the subroutine expects more than 1 parameter.  Then submit a
174       reference on an array as 'hash key'.  The tied subroutine will get the
175       parameters always as list.
176
177       Use any reference to give back more then 1 return value.  The caller
178       get back this reference.  There is no way to return a list.
179

SUBROUTINES/METHODS

181   method TIEHASH
182           use Tie::Sub;
183           my $object = tie my %subroutine, 'Tie::Sub', sub { yourcode };
184
185       'TIEHASH' ties your hash and set options defaults.
186
187   method config
188       'config' stores your own subroutine
189
190       You can get back the previous code reference or use the method config
191       in void context.  When you configure the first subroutine, the method
192       will give back undef.
193
194           $previous_coderef = ( tied %subroutine )->config( sub { yourcode } );
195
196       The method calls croak if you have a parameter and this parameter is
197       not a reference of 'CODE'.
198
199   method FETCH
200       Give your parameter as key of your tied hash.  This key can be a string
201       or an array reference when you have more then one.  'FETCH' will run
202       your tied subroutine and give back the returns of your subroutine.
203       Think about, return value can't be a list, but reference of such
204       things.
205
206           ... = $subroutine{param};
207

DIAGNOSTICS

209       All methods can croak at false parameters.
210

CONFIGURATION AND ENVIRONMENT

212       nothing
213

DEPENDENCIES

215       Carp
216
217       Params::Validate
218

INCOMPATIBILITIES

220       not known
221

BUGS AND LIMITATIONS

223       not known
224

SEE ALSO

226       Tie::Hash
227
228       <http://perl.plover.com/Identity/>
229
230       <http://perl.plover.com/Interpolation/>
231
232       Interpolation # contains much things
233
234       Tie::Function # maybe there is a problem near '$;' in your Arguments
235
236       Tie::LazyFunction
237

AUTHOR

239       Steffen Winkler
240
242       Copyright (c) 2005 - 2012, Steffen Winkler "<steffenw at cpan.org>".
243       All rights reserved.
244
245       This module is free software; you can redistribute it and/or modify it
246       under the same terms as Perl itself.
247
248
249
250perl v5.30.0                      2019-07-26                       Tie::Sub(3)
Impressum