#!/usr/bin/perl
my $obj = Multi->new() ;
my $string = $obj ;
$string .= '_x' ;
print "*** $string\n" ;
my $hash_a = $obj->{A} ;
print "$hash_a\n" ;
my $array_0 = $obj->[0] ;
print "$array_0\n" ;
$obj->method(qw(z y z)) ;
print "Me as scalar: $obj\n" ;
#########
# MULTI #
#########
package Multi ;
use Symbol ;
use overload (
'""' => 'string' ,
'=' => 'copy' ,
'0+' => 'copy' ,
'@{}' => 'get_array' ,
'%{}' => 'get_hash' ,
'fallback' => 1 ,
) ;
sub new {
my $class = shift ;
my $this = gensym ;
bless($this,$class) ;
my $scalar = 'Foo' ;
*$this = \$scalar ;
*$this = [qw(a b c)] ;
*$this = {A => 10 , B => 20 , C => 30} ;
return( $this ) ;
}
sub method {
my $this = shift ;
print "METHOD>> @_\n" ;
}
sub string {
my $this = shift ;
return( ${*$this} ) ;
}
sub copy {
my $this = shift ;
return( substr(${*$this} , 0 ) ) ;
}
sub get_array {
my $this = shift ;
return( \@{*$this} ) ;
}
sub get_hash {
my $this = shift ;
return( \%{*$this} ) ;
}
1;
####
my %hash ;
tie(%hash, 'TieHash' , $this) ;
*$this = \%hash ;
## In the place of:
## *$this = {A => 10 , B => 20 , C => 30} ;
####
$obj->{key} ;
$obj->[0] ;
print "Scalar: $obj" ;