1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
|
#############################################################################
## Name: MultiType.pm
## Purpose: Object::MultiType
## Author: Graciliano M. P.
## Modified by:
## Created: 10/05/2003
## RCS-ID:
## Copyright: (c) 2003 Graciliano M. P.
## Licence: This program is free software; you can redistribute it and/or
## modify it under the same terms as Perl itself
#############################################################################
package Object::MultiType;
use 5.006 ;
use strict qw(vars) ;
our $VERSION = '0.05';
no warnings ;
use overload (
'bool' => '_OVER_bool' ,
'""' => '_OVER_string' ,
'=' => '_OVER_copy' ,
'+' => '_OVER_inc' ,
'-' => '_OVER_deinc' ,
'0+' => '_OVER_copy' ,
'@{}' => '_OVER_get_array' ,
'%{}' => '_OVER_get_hash' ,
'&{}' => '_OVER_get_code' ,
'*{}' => '_OVER_get_glob' ,
'fallback' => 1 ,
) ;
sub is_saver { 0 ;}
#######
# NEW #
#######
sub new {
my $class = shift ;
my (%args) = @_ ;
my $saver = Object::MultiType::Saver->new( $args{nodefault} ) ;
my $this = \$saver ;
bless($this,$class) ;
if (!defined $args{boolsub} && defined $args{boolcode} ) { $args{boolsub} = $args{boolcode} ;}
if ( exists $args{bool} ) { $saver->set_bool($args{bool}) ;}
elsif ( $args{boolsub} ) {
my $sub = $args{boolsub} ;
$saver->set_bool(\$sub) ;
}
if (!defined $args{scalarsub} && defined $args{scalarcode} ) { $args{scalarsub} = $args{scalarcode} ;}
if ( defined $args{scalar} ) { $saver->set_scalar($args{scalar}) ;}
elsif ( $args{scalarsub} ) {
my $sub = $args{scalarsub} ;
$saver->set_scalar(\$sub) ;
}
if ( ref $args{array} eq 'ARRAY' ) { $saver->set_array($args{array}) ;}
elsif ( $args{tiearray} ) {
if ( $args{tieonuse} ) { $saver->{TIEONUSE}{a} = $args{tiearray} ;}
else {
my @array ; tie(@array,$args{tiearray},$$this) ;
$saver->set_array(\@array) ;
}
}
if ( ref $args{hash} eq 'HASH' ) { $saver->set_hash($args{hash}) ;}
elsif ( $args{tiehash} ) {
if ( $args{tieonuse} ) { $saver->{TIEONUSE}{h} = $args{tiehash} ;}
else {
my %hash = 1 ; tie(%hash,$args{tiehash},$$this) ;
$saver->set_hash(\%hash) ;
}
}
if ( ref $args{code} eq 'CODE' ) { $saver->set_code( $args{code} ) ;}
if ( $args{tiehandle} ) {
if (!$args{glob}) { local(*NULL) ; $args{glob} = \*NULL ;}
if ( $args{tieonuse} ) { $saver->{TIEONUSE}{g} = $args{tiehandle} ;}
else { tie($args{glob} , $args{tiehandle} , $$this) ;}
}
if ( ref $args{glob} eq 'GLOB' ) { $saver->set_glob( $args{glob} ) ;}
return( $this ) ;
}
##############
# _OVER_BOOL #
##############
sub _OVER_bool {
my $this = shift ;
if ( !exists $$this->{b} ) {
return $this->_OVER_string ;
}
my $bool = $$this->bool ;
if (ref($bool) && ref($$bool) eq 'CODE') {
my $sub = $$bool ;
return &$sub($this) ;
}
if (ref($bool) eq 'SCALAR') { return( $$bool ) ;}
return( $bool ) ;
}
##########
# STRING #
##########
sub _OVER_string {
my $this = shift ;
my $scalar = $$this->scalar ;
if (ref($$scalar) eq 'CODE') {
my $sub = $$scalar ;
return &$sub($this) ;
}
else { return( $$scalar ) ;}
}
#############
# _OVER_INC #
#############
sub _OVER_inc {
my $this = shift ;
my $scalar = $$this->scalar ;
my $n ;
if (ref($$scalar) eq 'CODE') {
my $sub = $$scalar ;
$n = &$sub($this) ;
}
else { $n = substr($$scalar , 0 ) ;}
$n += $_[0] ;
return $n ;
}
###############
# _OVER_DEINC #
###############
sub _OVER_deinc {
my $this = shift ;
my $scalar = $$this->scalar ;
my $n ;
if (ref($$scalar) eq 'CODE') {
my $sub = $$scalar ;
$n = &$sub($this) ;
}
else { $n = substr($$scalar , 0 ) ;}
$n -= $_[0] ;
return $n ;
}
##############
# _OVER_COPY #
##############
sub _OVER_copy {
my $this = shift ;
my $scalar = $$this->scalar ;
if (ref($$scalar) eq 'CODE') {
my $sub = $$scalar ;
return &$sub($this) ;
}
else { return( substr($$scalar , 0 ) ) ;}
}
#############
# GET_ARRAY #
#############
sub _OVER_get_array {
my $this = shift ;
if ( $$this->{TIEONUSE}{a} ) {
my @array ; tie(@array, $$this->{TIEONUSE}{a} , $$this) ;
$$this->set_array(\@array) ;
$$this->{TIEONUSE}{a} = undef ;
}
return( $$this->array ) ;
}
############
# GET_HASH #
############
sub _OVER_get_hash {
my $this = shift ;
if ( $$this->{TIEONUSE}{h} ) {
my %hash = 1 ; tie(%hash, $$this->{TIEONUSE}{h} ,$$this) ;
$$this->set_hash(\%hash) ;
$$this->{TIEONUSE}{h} = undef ;
}
return( $$this->hash ) ;
}
##################
# _OVER_GET_CODE #
##################
sub _OVER_get_code {
my $this = shift ;
if ( !$$this->{SUBCODE} ) {
$$this->{SUBCODE}{self} = undef ;
my $sub = $$this->code ;
my $ref = $$this->{SUBCODE} ;
$$this->{SUBCODE}{sub} = sub {
if (wantarray) {
my @ret = &$sub( $$ref{self} , @_) ;
$$ref{self} = undef ;
return( @ret ) ;
}
else {
my $ret = &$sub( $$ref{self} , @_) ;
$$ref{self} = undef ;
return( $ret ) ;
}
};
}
$$this->{SUBCODE}{self} = $this ;
return( $$this->{SUBCODE}{sub} ) ;
}
##################
# _OVER_GET_GLOB #
##################
sub _OVER_get_glob {
my $this = shift ;
if ( $$this->{TIEONUSE}{g} ) {
tie($$this->glob , $$this->{TIEONUSE}{g} , $$this) ;
$$this->{TIEONUSE}{g} = undef ;
}
return( $$this->glob ) ;
}
###########
# DESTROY #
###########
sub DESTROY {
my $this = shift ;
$$this->clean ;
}
############################
# OBJECT::MULTITYPE::SAVER #
############################
package Object::MultiType::Saver ;
use strict qw(vars) ;
sub is_saver { 1 ;}
sub new {
my $class = shift ;
my ( $nodefault ) = @_ ;
my $this ;
if ($nodefault) { $this = {} ;}
else {
local(*NULL);
$this = {
s => \'' ,
a => [] ,
h => {} ,
c => sub{} ,
g => \*NULL ,
} ;
}
bless($this,$class);
return( $this ) ;
}
sub bool { return( $_[0]->{b} ) ;}
sub scalar { return( $_[0]->{s} ) ;}
sub array { return( $_[0]->{a} ) ;}
sub hash { return( $_[0]->{h} ) ;}
sub code { return( $_[0]->{c} ) ;}
sub glob { return( $_[0]->{g} ) ;}
sub set_bool { $_[0]->{b} = $_[1] ;}
sub set_scalar {
if ($#_ == 0) { $_[0]->{s} = undef ;}
elsif (ref($_[1]) ne 'SCALAR' && ref($_[1]) ne 'REF') { $_[0]->{s} = \$_[1] ;}
else { $_[0]->{s} = $_[1] ;}
}
sub set_array { $_[0]->{a} = $_[1] ;}
sub set_hash { $_[0]->{h} = $_[1] ;}
sub set_code { $_[0]->{c} = $_[1] ;}
sub set_glob { $_[0]->{g} = $_[1] ;}
sub clean {
my $this = shift ;
$this->set_bool() ;
$this->set_scalar() ;
$this->set_array() ;
$this->set_hash() ;
$this->set_code() ;
$this->set_glob() ;
}
sub DESTROY { &clean ;}
#######
# END #
#######
1;
__END__
=head1 NAME
Object::MultiType - Perl Objects as Hash, Array, Scalar, Code and Glob at the same time.
=head1 SYNOPSIS
use Object::MultiType ;
my $scalar = 'abc' ;
my @array = qw(x y z);
my %hash = (A => 1 , B => 2) ;
my $obj = Object::MultiType->new(
scalar => \$scalar ,
array => \@array ,
hash => \%hash ,
code => sub{ return("I'm a sub ref!") ; }
glob => \*STDOUT ,
) ;
print "Me as scalar: $obj\n" ;
my $array_1 = $obj->[1] ;
print "$array_1\n" ;
my $hash_B = $obj->{B} ;
print "$hash_B\n" ;
my $hash = $$obj->hash ;
foreach my $Key (sort keys %$hash ) {
print "$Key = $$hash{$Key}\n" ;
}
&$obj(args) ;
=head1 DESCRIPTION
This module return an object that works like a Hash, Array, Scalar, Code and Glob object at the same time.
The usual way is to call it from your module at new():
package FOO ;
use Object::MultiType ;
use vars qw(@ISA) ;
@ISA = qw(Object::MultiType) ; ## Is good to 'Object::MultiType' be the last in @ISA!
sub new {
my $class = shift ;
my $this = Object::MultiType->new() ;
bless($this,$class) ;
}
=head1 METHODS
** See the methods of the L<Saver|/SAVER> too.
=head2 new
B<Arguments>:
=over 10
=item bool
The I<boolean> reference. Default: undef
=item boolcode|boolsub
Set the sub/function (CODE reference) that will return/generate the I<boolean> value.
=item scalar
The SCALAR reference. If not sent a null SCALAR will be created.
=item scalarcode|scalarsub
Set the sub/function (CODE reference) that will return/generate the scalar data of the object.
=item array
The ARRAY reference. If not sent a null ARRAY will be created.
=item hash
The HASH reference. If not sent a null HASH will be created.
=item code
The CODE reference. If not sent a null sub{} will be created.
With this your object can be used as a sub reference:
my $multi = Object::MultiType->new( code => sub { print "Args: @_\n" ;} ) ;
&$multi();
Note that the first argument sent to the SUB is the object ($multi).
=item glob
The GLOB (HANDLE) reference. If not sent a null GLOB will be created.
** Note that you can't use the default (null) GLOB created when you don't paste this argument!
Since all the objects will share it, and was there just to avoid erros!
=item tiearray
Package name to create a TIEARRAY. The argument $$this is sent to tie().
tie() is called as:
tie(@array,$args{tiearray},$$this) ;
Note that is hard to implement the tie methods for PUSH, POP, SHIFT, UNSHIFT, SPLICE...
Unless you make just an alias to another array through the tie methods.
** See B<tiehash> too.
=item tiehash
Package name to create a TIEHASH. The argument $$this is sent to tie().
tie() is called as:
tie(%hash,$args{tiehash},$$this) ;
** $$this (the Saver) is sent, and not $this, to avoid the break of DESTROY (auto reference).
** $$this is a reference to the Saver object that save the SCALAR, ARRAY, HASH, CODE and GLOB.
sub TIEHASH {
my $class = shift ;
my $multi = shift ; ## $$this
my $scalarref = $multi->scalar ; ## \${*$multi}
my $arrayref = $multi->array ; ## \@{*$multi}
my $hashref = $multi->hash ; ## \%{*$multi}
my $this = { s => $scalarref , a => $arrayref , h => $hashref } ;
bless($this,$class) ;
}
=item tiehandle
Make the object works like a tied glob (TIEHANDLE).
If used with I<glob> will tie() it. If I<glob> is not sent a NULL GLOB is used:
my $multi = Object::MultiType->new(
glob => \*MYOUT , ## 'glob' is Optional.
tiehandle => 'TieHandlePack' ,
) ;
=item tieonuse
The reference is only tied when it's used! So, the HASH, ARRAY or GLOB (handle)
are only tied if/when they are accessed.
=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).
** This is good to save memory.
=back
=head2 is_saver
Return 0. Good to see if what you have is the Saver or the MultiType object.
=head1 SAVER
The MultiType object has a Saver object (L<Object::MultiType::Saver|/Object::MultiType::Saver>),
that save all the different data type (references). This saver can be accessed from the main object:
my $multi = Object::MultiType->new() ;
my $saver = $$multi ;
print $saver->scalar ;
B<If you want to save attributes in your Object and you use I<tiehash>, you can't set attributes directly in the MultiType object>!:
sub new {
my $class = shift ;
my $this = Object::MultiType->new(tiehash => 'TieHashPack') ;
## Dont do that! This will call the STORE() at TIEHASH, and not save it in the object:
$this->{flagx} = 1 ;
bless($this,$class) ;
}
So, if you use tiehash and want to save attributes (outside tie) use that:
## This save the attribute inside the Saver:
$$this->{flagx} = 1 ;
Note that this set an attribute in the saver, and it has their own attributes!
## $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.
** See I<"Direct access to the data types">.
=head1 DESTROY
When the object is DESTROIED, the Saver inside it is cleanned, so the tied objects can be DESTROIED automatically too.
=head1 Direct access to the data types
To access directly the reference of the different data types (SCALAR, ARRAY, HASH, CODE & GLOB) use:
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 ;
Setting the data:
$saver->set_bool( 1 ) ;
$saver->set_scalar( 'xyz' ) ;
$saver->set_array( [qw(x y z)] ) ;
$saver->set_hash( {X => 1} ) ;
$saver->set_code( sub{ print "XYZ\n" ; } ) ;
$saver->set_glob( \*STDOUT ) ;
=head1 As SCALAR
You can use it as SCALAR when you put it inside quotes or make a copy of it:
my $multi = Object::MultiType->new( scalar => 'Foo' ) ;
## Quote:
print "Me as scalar: $multi\n" ;
## Copy:
my $str = $multi ;
$str .= '_x' ; ## Copy made when you change it! Until that $str works like $multi.
print "$str\n" ;
using the argument B<scalarsub> you can use a function that will generate the scalar data,
in the place of a reference to a SCALAR:
my $multi = Object::MultiType->new(scalarsub => sub{ return 'generated data' ;} ) ;
print "My scalar have $multi!\n" ;
=head1 As ARRAY
You can use it as ARRAY directly from the object:
my $multi = Object::MultiType->new( array => [qw(FOO BAR)] ) ;
my $array_0 = $multi->[0] ;
$multi->[1] = 'foo' ;
=head1 As HASH
You can use it as HASH directly from the object:
my $multi = Object::MultiType->new( hash => {key => 'foo'} ) ;
my $k = $multi->{key} ;
$multi->{foo} = 'bar' ;
=head1 With TIE
To use your ARRAY and HASH part tied, you can paste the reference already tied of the HASH or ARRAY,
or use the arguments tiehash and tiearray at L<new()|/new>:
## Using the reference:
my %hash ;
tie(%hash,'TieHash') ;
my $multi = Object::MultiType->new(hash => \%hash) ;
## Or using directly the argument:
my $multi = Object::MultiType->new(tiehash => 'TieHashPack') ;
Note that using tiehash or tiearray is better, since your tied HASH or ARRAY can see the object Saver and
the other data type of it. B<See the method L<new()|/new> and their arguments>.
Here's an example of a TieHash package that is called from Object::MultiType->new():
## 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( 'key' ) ;}
sub NEXTKEY { my $this = shift ; return( 'key' ) ;}
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 {}
B<Using tiehash, you need to save the attributes in the Saver, or you call the tie()>.
$$this->{flagx} = 1 ;
=head1 Object::MultiType::Saver
This is a litte package where the Saver objects are created.
It will save the data types (SCALAR, ARRAY, HASH, CODE & GLOB) of the main objects (Object::MultiType).
B<METHODS:>
=head2 is_saver
Return 1. Good to see if what you have is the Saver or the MultiType object.
=head2 bool
Return the BOOL reference inside the Saver.
=head2 scalar
Return the SCALAR reference inside the Saver.
=head2 array
Return the ARRAY reference inside the Saver.
=head2 hash
Return the HASH reference inside the Saver.
=head2 code
Return the CODE/sub reference inside the Saver.
=head2 glob
Return the GLOB/HANDLE reference inside the Saver.
=head2 set_bool
Set the boolean reference inside the Saver.
=head2 set_scalar
Set the SCALAR reference inside the Saver.
=head2 set_array
Set the ARRAY reference inside the Saver.
=head2 set_hash
Set the HASH reference inside the Saver.
=head2 set_code
Set the CODE/sub reference inside the Saver.
=head2 set_glob
Set the GLOB/HANDLE reference inside the Saver.
=head2 clean
Clean all the references saved in the Saver.
=head1 SEE ALSO
L<overload>, L<perltie>, L<Scalar::Util>.
This module/class was created for L<XML::Smart>.
=head1 AUTHOR
Graciliano M. P. <gm@virtuasites.com.br>
I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P
=head1 COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
|