.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" 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 .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . 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 .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "MultiType 3pm" .TH MultiType 3pm "2018-01-18" "perl v5.26.1" "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" Object::MultiType \- Perl Objects as Hash, Array, Scalar, Code and Glob at the same time. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Object::MultiType ; \& \& my $scalar = \*(Aqabc\*(Aq ; \& my @array = qw(x y z); \& my %hash = (A => 1 , B => 2) ; \& \& my $obj = Object::MultiType\->new( \& scalar => \e$scalar , \& array => \e@array , \& hash => \e%hash , \& code => sub{ return("I\*(Aqm a sub ref!") ; } \& glob => \e*STDOUT , \& ) ; \& \& print "Me as scalar: $obj\en" ; \& \& my $array_1 = $obj\->[1] ; \& print "$array_1\en" ; \& \& my $hash_B = $obj\->{B} ; \& print "$hash_B\en" ; \& \& my $hash = $$obj\->hash ; \& foreach my $Key (sort keys %$hash ) { \& print "$Key = $$hash{$Key}\en" ; \& } \& \& &$obj(args) ; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module return an object that works like a Hash, Array, Scalar, Code and Glob object at the same time. .PP The usual way is to call it from your module at \fInew()\fR: .PP .Vb 1 \& package FOO ; \& \& use Object::MultiType ; \& use vars qw(@ISA) ; \& @ISA = qw(Object::MultiType) ; ## Is good to \*(AqObject::MultiType\*(Aq be the last in @ISA! \& \& sub new { \& my $class = shift ; \& my $this = Object::MultiType\->new() ; \& bless($this,$class) ; \& } .Ve .SH "METHODS" .IX Header "METHODS" ** See the methods of the Saver too. .SS "new" .IX Subsection "new" \&\fBArguments\fR: .IP "bool" 10 .IX Item "bool" The \fIboolean\fR reference. Default: undef .IP "boolcode|boolsub" 10 .IX Item "boolcode|boolsub" Set the sub/function (\s-1CODE\s0 reference) that will return/generate the \fIboolean\fR value. .IP "scalar" 10 .IX Item "scalar" The \s-1SCALAR\s0 reference. If not sent a null \s-1SCALAR\s0 will be created. .IP "scalarcode|scalarsub" 10 .IX Item "scalarcode|scalarsub" Set the sub/function (\s-1CODE\s0 reference) that will return/generate the scalar data of the object. .IP "array" 10 .IX Item "array" The \s-1ARRAY\s0 reference. If not sent a null \s-1ARRAY\s0 will be created. .IP "hash" 10 .IX Item "hash" The \s-1HASH\s0 reference. If not sent a null \s-1HASH\s0 will be created. .IP "code" 10 .IX Item "code" The \s-1CODE\s0 reference. If not sent a null sub{} will be created. .Sp With this your object can be used as a sub reference: .Sp .Vb 2 \& my $multi = Object::MultiType\->new( code => sub { print "Args: @_\en" ;} ) ; \& &$multi(); .Ve .Sp Note that the first argument sent to the \s-1SUB\s0 is the object ($multi). .IP "glob" 10 .IX Item "glob" The \s-1GLOB\s0 (\s-1HANDLE\s0) reference. If not sent a null \s-1GLOB\s0 will be created. .Sp ** Note that you can't use the default (null) \s-1GLOB\s0 created when you don't paste this argument! Since all the objects will share it, and was there just to avoid erros! .IP "tiearray" 10 .IX Item "tiearray" Package name to create a \s-1TIEARRAY.\s0 The argument $$this is sent to \fItie()\fR. .Sp \&\fItie()\fR is called as: .Sp .Vb 1 \& tie(@array,$args{tiearray},$$this) ; .Ve .Sp Note that is hard to implement the tie methods for \s-1PUSH, POP, SHIFT, UNSHIFT, SPLICE...\s0 Unless you make just an alias to another array through the tie methods. .Sp ** See \fBtiehash\fR too. .IP "tiehash" 10 .IX Item "tiehash" Package name to create a \s-1TIEHASH.\s0 The argument $$this is sent to \fItie()\fR. .Sp \&\fItie()\fR is called as: .Sp .Vb 1 \& tie(%hash,$args{tiehash},$$this) ; .Ve .Sp ** $$this (the Saver) is sent, and not \f(CW$this\fR, to avoid the break of \s-1DESTROY\s0 (auto reference). .Sp ** $$this is a reference to the Saver object that save the \s-1SCALAR, ARRAY, HASH, CODE\s0 and \s-1GLOB.\s0 .Sp .Vb 3 \& sub TIEHASH { \& my $class = shift ; \& my $multi = shift ; ## $$this \& \& my $scalarref = $multi\->scalar ; ## \e${*$multi} \& my $arrayref = $multi\->array ; ## \e@{*$multi} \& my $hashref = $multi\->hash ; ## \e%{*$multi} \& \& my $this = { s => $scalarref , a => $arrayref , h => $hashref } ; \& bless($this,$class) ; \& } .Ve .IP "tiehandle" 10 .IX Item "tiehandle" Make the object works like a tied glob (\s-1TIEHANDLE\s0). .Sp If used with \fIglob\fR will \fItie()\fR it. If \fIglob\fR is not sent a \s-1NULL GLOB\s0 is used: .Sp .Vb 4 \& my $multi = Object::MultiType\->new( \& glob => \e*MYOUT , ## \*(Aqglob\*(Aq is Optional. \& tiehandle => \*(AqTieHandlePack\*(Aq , \& ) ; .Ve .IP "tieonuse" 10 .IX Item "tieonuse" The reference is only tied when it's used! So, the \s-1HASH, ARRAY\s0 or \s-1GLOB\s0 (handle) are only tied if/when they are accessed. .IP "nodefault" 10 .IX Item "nodefault" If set to true tell to not create the default references inside the Saver, and it will have only the references paste (scalar, array, hash, code, glob). .Sp ** This is good to save memory. .SS "is_saver" .IX Subsection "is_saver" Return 0. Good to see if what you have is the Saver or the MultiType object. .SH "SAVER" .IX Header "SAVER" The MultiType object has a Saver object (Object::MultiType::Saver), that save all the different data type (references). This saver can be accessed from the main object: .PP .Vb 1 \& my $multi = Object::MultiType\->new() ; \& \& my $saver = $$multi ; \& print $saver\->scalar ; .Ve .PP \&\fBIf you want to save attributes in your Object and you use \f(BItiehash\fB, you can't set attributes directly in the MultiType object\fR!: .PP .Vb 3 \& sub new { \& my $class = shift ; \& my $this = Object::MultiType\->new(tiehash => \*(AqTieHashPack\*(Aq) ; \& \& ## Dont do that! This will call the STORE() at TIEHASH, and not save it in the object: \& $this\->{flagx} = 1 ; \& \& bless($this,$class) ; \& } .Ve .PP So, if you use tiehash and want to save attributes (outside tie) use that: .PP .Vb 2 \& ## This save the attribute inside the Saver: \& $$this\->{flagx} = 1 ; .Ve .PP Note that this set an attribute in the saver, and it has their own attributes! .PP .Vb 1 \& ## $saver = $$this ; \& \& $saver\->{s} ## the sacalar ref. \& $saver\->{a} ## the array ref. \& $saver\->{h} ## the hash ref. \& $saver\->{c} ## the code ref. \& $saver\->{g} ## the glob ref. .Ve .PP ** See \fI\*(L"Direct access to the data types\*(R"\fR. .SH "DESTROY" .IX Header "DESTROY" When the object is \s-1DESTROIED,\s0 the Saver inside it is cleanned, so the tied objects can be \s-1DESTROIED\s0 automatically too. .SH "Direct access to the data types" .IX Header "Direct access to the data types" To access directly the reference of the different data types (\s-1SCALAR, ARRAY, HASH, CODE & GLOB\s0) use: .PP .Vb 1 \& my $multi = Object::MultiType\->new() ; \& \& my $saver = $$multi ; \& \& my $scalarref = $saver\->scalar ; ## $saver\->{s} \& my $arrayref = $saver\->array ; ## $saver\->{a} \& my $hashref = $saver\->hash ; ## $saver\->{h} \& my $coderef = $saver\->code ; ## $saver\->{c} \& my $globeref = $saver\->glob ; ## $saver\->{g} \& \& ## You can access the Saver directly from the main object: \& $$multi\->hash ; .Ve .PP Setting the data: .PP .Vb 6 \& $saver\->set_bool( 1 ) ; \& $saver\->set_scalar( \*(Aqxyz\*(Aq ) ; \& $saver\->set_array( [qw(x y z)] ) ; \& $saver\->set_hash( {X => 1} ) ; \& $saver\->set_code( sub{ print "XYZ\en" ; } ) ; \& $saver\->set_glob( \e*STDOUT ) ; .Ve .SH "As SCALAR" .IX Header "As SCALAR" You can use it as \s-1SCALAR\s0 when you put it inside quotes or make a copy of it: .PP .Vb 1 \& my $multi = Object::MultiType\->new( scalar => \*(AqFoo\*(Aq ) ; \& \& ## Quote: \& print "Me as scalar: $multi\en" ; \& \& ## Copy: \& my $str = $multi ; \& $str .= \*(Aq_x\*(Aq ; ## Copy made when you change it! Until that $str works like $multi. \& print "$str\en" ; .Ve .PP using the argument \fBscalarsub\fR you can use a function that will generate the scalar data, in the place of a reference to a \s-1SCALAR:\s0 .PP .Vb 1 \& my $multi = Object::MultiType\->new(scalarsub => sub{ return \*(Aqgenerated data\*(Aq ;} ) ; \& \& print "My scalar have $multi!\en" ; .Ve .SH "As ARRAY" .IX Header "As ARRAY" You can use it as \s-1ARRAY\s0 directly from the object: .PP .Vb 3 \& my $multi = Object::MultiType\->new( array => [qw(FOO BAR)] ) ; \& my $array_0 = $multi\->[0] ; \& $multi\->[1] = \*(Aqfoo\*(Aq ; .Ve .SH "As HASH" .IX Header "As HASH" You can use it as \s-1HASH\s0 directly from the object: .PP .Vb 3 \& my $multi = Object::MultiType\->new( hash => {key => \*(Aqfoo\*(Aq} ) ; \& my $k = $multi\->{key} ; \& $multi\->{foo} = \*(Aqbar\*(Aq ; .Ve .SH "With TIE" .IX Header "With TIE" To use your \s-1ARRAY\s0 and \s-1HASH\s0 part tied, you can paste the reference already tied of the \s-1HASH\s0 or \s-1ARRAY,\s0 or use the arguments tiehash and tiearray at \fInew()\fR: .PP .Vb 4 \& ## Using the reference: \& my %hash ; \& tie(%hash,\*(AqTieHash\*(Aq) ; \& my $multi = Object::MultiType\->new(hash => \e%hash) ; \& \& ## Or using directly the argument: \& my $multi = Object::MultiType\->new(tiehash => \*(AqTieHashPack\*(Aq) ; .Ve .PP Note that using tiehash or tiearray is better, since your tied \s-1HASH\s0 or \s-1ARRAY\s0 can see the object Saver and the other data type of it. \fBSee the method \f(BInew()\fB and their arguments\fR. .PP Here's an example of a TieHash package that is called from Object::MultiType\->\fInew()\fR: .PP .Vb 2 \& ## The call inside Object::MultiType\->new(): \& tie(%hash,$args{tiehash},$$this) ; \& \& ## The package: \& package TieHash ; \& \& sub TIEHASH { \& my $class = shift ; \& my $Saver = shift ; ## Object::MultiType paste as $$this (only the Saver) to avoid break of DESTROY! \& ## $this = Object::MultiType >> $$this = Object::MultiType::Saver \& \& my $scalarref = $Saver\->scalar ; \& my $arrayref = $Saver\->array ; \& \& ## Note that $Saver\->hash will return the tied hash, and is not needed here! \& ## my $hashref = $Saver\->hash ; \& \& ## Saving the references inside the TIE object: \& my $this = { scalar => $scalarref , array => $arrayref , hash => {} } ; \& \& bless($this,$class) ; \& } \& \& sub FETCH { my $this = shift ; return( \*(Aqkey\*(Aq ) ;} \& \& sub NEXTKEY { my $this = shift ; return( \*(Aqkey\*(Aq ) ;} \& \& sub STORE { my $this = shift ; $this\->{hash}{$_[0]} = $_[1] } \& \& sub DELETE { my $this = shift ; delete $this\->{hash}{$_[0]} } \& \& sub CLEAR { my $this = shift ; $this\->{hash} = {} ;} \& \& sub EXISTS { my $this = shift ; defined $this\->{hash}{$_[0]} ;} \& \& sub FIRSTKEY { my $this = shift ; (sort keys %{$this\->{hash}} )[0] } \& \& sub DESTROY {} .Ve .PP \&\fBUsing tiehash, you need to save the attributes in the Saver, or you call the \f(BItie()\fB\fR. .PP .Vb 1 \& $$this\->{flagx} = 1 ; .Ve .SH "Object::MultiType::Saver" .IX Header "Object::MultiType::Saver" This is a litte package where the Saver objects are created. It will save the data types (\s-1SCALAR, ARRAY, HASH, CODE & GLOB\s0) of the main objects (Object::MultiType). .PP \&\fB\s-1METHODS:\s0\fR .SS "is_saver" .IX Subsection "is_saver" Return 1. Good to see if what you have is the Saver or the MultiType object. .SS "bool" .IX Subsection "bool" Return the \s-1BOOL\s0 reference inside the Saver. .SS "scalar" .IX Subsection "scalar" Return the \s-1SCALAR\s0 reference inside the Saver. .SS "array" .IX Subsection "array" Return the \s-1ARRAY\s0 reference inside the Saver. .SS "hash" .IX Subsection "hash" Return the \s-1HASH\s0 reference inside the Saver. .SS "code" .IX Subsection "code" Return the CODE/sub reference inside the Saver. .SS "glob" .IX Subsection "glob" Return the \s-1GLOB/HANDLE\s0 reference inside the Saver. .SS "set_bool" .IX Subsection "set_bool" Set the boolean reference inside the Saver. .SS "set_scalar" .IX Subsection "set_scalar" Set the \s-1SCALAR\s0 reference inside the Saver. .SS "set_array" .IX Subsection "set_array" Set the \s-1ARRAY\s0 reference inside the Saver. .SS "set_hash" .IX Subsection "set_hash" Set the \s-1HASH\s0 reference inside the Saver. .SS "set_code" .IX Subsection "set_code" Set the CODE/sub reference inside the Saver. .SS "set_glob" .IX Subsection "set_glob" Set the \s-1GLOB/HANDLE\s0 reference inside the Saver. .SS "clean" .IX Subsection "clean" Clean all the references saved in the Saver. .SH "SEE ALSO" .IX Header "SEE ALSO" overload, perltie, Scalar::Util. .PP This module/class was created for XML::Smart. .SH "AUTHOR" .IX Header "AUTHOR" Graciliano M. P. .PP I will appreciate any type of feedback (include your opinions and/or suggestions). ;\-P .SH "COPYRIGHT" .IX Header "COPYRIGHT" This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.