1MultiType(3)          User Contributed Perl Documentation         MultiType(3)
2
3
4

NAME

6       Object::MultiType - Perl Objects as Hash, Array, Scalar, Code and Glob
7       at the same time.
8

SYNOPSIS

10         use Object::MultiType ;
11
12         my $scalar = 'abc' ;
13         my @array  = qw(x y z);
14         my %hash   = (A => 1 , B => 2) ;
15
16         my $obj = Object::MultiType->new(
17         scalar => \$scalar ,
18         array  => \@array ,
19         hash   => \%hash ,
20         code   => sub{ return("I'm a sub ref!") ; }
21         glob   => \*STDOUT ,
22         ) ;
23
24         print "Me as scalar: $obj\n" ;
25
26         my $array_1 = $obj->[1] ;
27         print "$array_1\n" ;
28
29         my $hash_B = $obj->{B} ;
30         print "$hash_B\n" ;
31
32         my $hash = $$obj->hash ;
33         foreach my $Key (sort keys %$hash ) {
34           print "$Key = $$hash{$Key}\n" ;
35         }
36
37         &$obj(args) ;
38

DESCRIPTION

40       This module return an object that works like a Hash, Array, Scalar,
41       Code and Glob object at the same time.
42
43       The usual way is to call it from your module at new():
44
45         package FOO ;
46
47         use Object::MultiType ;
48         use vars qw(@ISA) ;
49         @ISA = qw(Object::MultiType) ; ## Is good to 'Object::MultiType' be the last in @ISA!
50
51         sub new {
52           my $class = shift ;
53           my $this = Object::MultiType->new() ;
54           bless($this,$class) ;
55         }
56

METHODS

58       ** See the methods of the Saver too.
59
60   new
61       Arguments:
62
63       bool      The boolean reference. Default: undef
64
65       boolcode|boolsub
66                 Set the sub/function (CODE reference) that will
67                 return/generate the boolean value.
68
69       scalar    The SCALAR reference. If not sent a null SCALAR will be
70                 created.
71
72       scalarcode|scalarsub
73                 Set the sub/function (CODE reference) that will
74                 return/generate the scalar data of the object.
75
76       array     The ARRAY reference. If not sent a null ARRAY will be
77                 created.
78
79       hash      The HASH reference. If not sent a null HASH will be created.
80
81       code      The CODE reference. If not sent a null sub{} will be created.
82
83                 With this your object can be used as a sub reference:
84
85                   my $multi = Object::MultiType->new( code => sub { print "Args: @_\n" ;} ) ;
86                   &$multi();
87
88                 Note that the first argument sent to the SUB is the object
89                 ($multi).
90
91       glob      The GLOB (HANDLE) reference. If not sent a null GLOB will be
92                 created.
93
94                 ** Note that you can't use the default (null) GLOB created
95                 when you don't paste this argument!  Since all the objects
96                 will share it, and was there just to avoid erros!
97
98       tiearray  Package name to create a TIEARRAY. The argument $$this is
99                 sent to tie().
100
101                 tie() is called as:
102
103                   tie(@array,$args{tiearray},$$this) ;
104
105                 Note that is hard to implement the tie methods for PUSH, POP,
106                 SHIFT, UNSHIFT, SPLICE...  Unless you make just an alias to
107                 another array through the tie methods.
108
109                 ** See tiehash too.
110
111       tiehash   Package name to create a TIEHASH. The argument $$this is sent
112                 to tie().
113
114                 tie() is called as:
115
116                   tie(%hash,$args{tiehash},$$this) ;
117
118                 ** $$this (the Saver) is sent, and not $this, to avoid the
119                 break of DESTROY (auto reference).
120
121                 ** $$this is a reference to the Saver object that save the
122                 SCALAR, ARRAY, HASH, CODE and GLOB.
123
124                   sub TIEHASH {
125                     my $class = shift ;
126                     my $multi = shift ; ## $$this
127
128                     my $scalarref = $multi->scalar ; ## \${*$multi}
129                     my $arrayref  = $multi->array  ; ## \@{*$multi}
130                     my $hashref   = $multi->hash   ; ## \%{*$multi}
131
132                     my $this = { s => $scalarref , a => $arrayref , h => $hashref } ;
133                     bless($this,$class) ;
134                   }
135
136       tiehandle Make the object works like a tied glob (TIEHANDLE).
137
138                 If used with glob will tie() it. If glob is not sent a NULL
139                 GLOB is used:
140
141                   my $multi = Object::MultiType->new(
142                   glob      => \*MYOUT ,               ## 'glob' is Optional.
143                   tiehandle => 'TieHandlePack' ,
144                   ) ;
145
146       tieonuse  The reference is only tied when it's used! So, the HASH,
147                 ARRAY or GLOB (handle) are only tied if/when they are
148                 accessed.
149
150       nodefault If set to true tell to not create the default references
151                 inside the Saver, and it will have only the references paste
152                 (scalar, array, hash, code, glob).
153
154                 ** This is good to save memory.
155
156   is_saver
157       Return 0. Good to see if what you have is the Saver or the MultiType
158       object.
159

SAVER

161       The MultiType object has a Saver object (Object::MultiType::Saver),
162       that save all the different data type (references). This saver can be
163       accessed from the main object:
164
165         my $multi = Object::MultiType->new() ;
166
167         my $saver = $$multi ;
168         print $saver->scalar ;
169
170       If you want to save attributes in your Object and you use tiehash, you
171       can't set attributes directly in the MultiType object!:
172
173         sub new {
174           my $class = shift ;
175           my $this = Object::MultiType->new(tiehash => 'TieHashPack') ;
176
177           ## Dont do that! This will call the STORE() at TIEHASH, and not save it in the object:
178           $this->{flagx} = 1 ;
179
180           bless($this,$class) ;
181         }
182
183       So, if you use tiehash and want to save attributes (outside tie) use
184       that:
185
186           ## This save the attribute inside the Saver:
187           $$this->{flagx} = 1 ;
188
189       Note that this set an attribute in the saver, and it has their own
190       attributes!
191
192         ## $saver = $$this ;
193
194         $saver->{s} ## the sacalar ref.
195         $saver->{a} ## the array ref.
196         $saver->{h} ## the hash ref.
197         $saver->{c} ## the code ref.
198         $saver->{g} ## the glob ref.
199
200       ** See "Direct access to the data types".
201

DESTROY

203       When the object is DESTROIED, the Saver inside it is cleanned, so the
204       tied objects can be DESTROIED automatically too.
205

Direct access to the data types

207       To access directly the reference of the different data types (SCALAR,
208       ARRAY, HASH, CODE & GLOB) use:
209
210         my $multi = Object::MultiType->new() ;
211
212         my $saver = $$multi ;
213
214         my $scalarref = $saver->scalar ; ## $saver->{s}
215         my $arrayref  = $saver->array  ; ## $saver->{a}
216         my $hashref   = $saver->hash   ; ## $saver->{h}
217         my $coderef   = $saver->code   ; ## $saver->{c}
218         my $globeref  = $saver->glob   ; ## $saver->{g}
219
220         ## You can access the Saver directly from the main object:
221         $$multi->hash  ;
222
223       Setting the data:
224
225         $saver->set_bool( 1 ) ;
226         $saver->set_scalar( 'xyz' ) ;
227         $saver->set_array( [qw(x y z)] ) ;
228         $saver->set_hash( {X => 1} ) ;
229         $saver->set_code( sub{ print "XYZ\n" ; } ) ;
230         $saver->set_glob( \*STDOUT ) ;
231

As SCALAR

233       You can use it as SCALAR when you put it inside quotes or make a copy
234       of it:
235
236         my $multi = Object::MultiType->new( scalar => 'Foo' ) ;
237
238         ## Quote:
239         print "Me as scalar: $multi\n" ;
240
241         ## Copy:
242         my $str = $multi ;
243         $str .= '_x' ; ## Copy made when you change it! Until that $str works like $multi.
244         print "$str\n" ;
245
246       using the argument scalarsub you can use a function that will generate
247       the scalar data, in the place of a reference to a SCALAR:
248
249         my $multi = Object::MultiType->new(scalarsub => sub{ return 'generated data' ;} ) ;
250
251         print "My scalar have $multi!\n" ;
252

As ARRAY

254       You can use it as ARRAY directly from the object:
255
256         my $multi = Object::MultiType->new( array => [qw(FOO BAR)] ) ;
257         my $array_0 = $multi->[0] ;
258         $multi->[1] = 'foo' ;
259

As HASH

261       You can use it as HASH directly from the object:
262
263         my $multi = Object::MultiType->new( hash => {key => 'foo'} ) ;
264         my $k = $multi->{key} ;
265         $multi->{foo} = 'bar' ;
266

With TIE

268       To use your ARRAY and HASH part tied, you can paste the reference
269       already tied of the HASH or ARRAY, or use the arguments tiehash and
270       tiearray at new():
271
272         ## Using the reference:
273         my %hash ;
274         tie(%hash,'TieHash') ;
275         my $multi = Object::MultiType->new(hash => \%hash) ;
276
277         ## Or using directly the argument:
278         my $multi = Object::MultiType->new(tiehash => 'TieHashPack') ;
279
280       Note that using tiehash or tiearray is better, since your tied HASH or
281       ARRAY can see the object Saver and the other data type of it. See the
282       method new() and their arguments.
283
284       Here's an example of a TieHash package that is called from
285       Object::MultiType->new():
286
287         ## The call inside Object::MultiType->new():
288         tie(%hash,$args{tiehash},$$this) ;
289
290         ## The package:
291         package TieHash ;
292
293         sub TIEHASH {
294             my $class = shift ;
295             my $Saver = shift ; ## Object::MultiType paste as $$this (only the Saver) to avoid break of DESTROY!
296                                 ## $this = Object::MultiType >> $$this = Object::MultiType::Saver
297
298             my $scalarref = $Saver->scalar ;
299             my $arrayref  = $Saver->array  ;
300
301             ## Note that $Saver->hash will return the tied hash, and is not needed here!
302             ## my $hashref   = $Saver->hash ;
303
304             ## Saving the references inside the TIE object:
305             my $this = { scalar => $scalarref , array => $arrayref , hash => {} } ;
306
307             bless($this,$class) ;
308         }
309
310         sub FETCH    { my $this = shift ; return( 'key' ) ;}
311
312         sub NEXTKEY  { my $this = shift ; return( 'key' ) ;}
313
314         sub STORE    { my $this = shift ; $this->{hash}{$_[0]} = $_[1] }
315
316         sub DELETE   { my $this = shift ; delete $this->{hash}{$_[0]} }
317
318         sub CLEAR    { my $this = shift ; $this->{hash} = {} ;}
319
320         sub EXISTS   { my $this = shift ; defined $this->{hash}{$_[0]} ;}
321
322         sub FIRSTKEY { my $this = shift ; (sort keys %{$this->{hash}} )[0] }
323
324         sub DESTROY  {}
325
326       Using tiehash, you need to save the attributes in the Saver, or you
327       call the tie().
328
329           $$this->{flagx} = 1 ;
330

Object::MultiType::Saver

332       This is a litte package where the Saver objects are created.  It will
333       save the data types (SCALAR, ARRAY, HASH, CODE & GLOB) of the main
334       objects (Object::MultiType).
335
336       METHODS:
337
338   is_saver
339       Return 1. Good to see if what you have is the Saver or the MultiType
340       object.
341
342   bool
343       Return the BOOL reference inside the Saver.
344
345   scalar
346       Return the SCALAR reference inside the Saver.
347
348   array
349       Return the ARRAY reference inside the Saver.
350
351   hash
352       Return the HASH reference inside the Saver.
353
354   code
355       Return the CODE/sub reference inside the Saver.
356
357   glob
358       Return the GLOB/HANDLE reference inside the Saver.
359
360   set_bool
361       Set the boolean reference inside the Saver.
362
363   set_scalar
364       Set the SCALAR reference inside the Saver.
365
366   set_array
367       Set the ARRAY reference inside the Saver.
368
369   set_hash
370       Set the HASH reference inside the Saver.
371
372   set_code
373       Set the CODE/sub reference inside the Saver.
374
375   set_glob
376       Set the GLOB/HANDLE reference inside the Saver.
377
378   clean
379       Clean all the references saved in the Saver.
380

SEE ALSO

382       overload, perltie, Scalar::Util.
383
384       This module/class was created for XML::Smart.
385

AUTHOR

387       Graciliano M. P. <gm@virtuasites.com.br>
388
389       I will appreciate any type of feedback (include your opinions and/or
390       suggestions). ;-P
391
393       This program is free software; you can redistribute it and/or modify it
394       under the same terms as Perl itself.
395
396
397
398perl v5.36.0                      2022-07-22                      MultiType(3)
Impressum