#---Sample.pm---
package Sample;
use warnings;
use strict;
use overload( '+' => \&add,
fallback => 1, );
sub TIESCALAR
{
my $class = shift;
my $self = { data => [@_], };
bless( $self, $class );
return $self;
}
sub add
{
$_ .= $_[1] foreach( @{$_[0]->{data}} );
}
sub STORE
{
@{$_[0]->{data}} = (ref( $_[1] ) eq 'ARRAY') ? @{$_[1]}: ($_[1]);
}
sub FETCH
{
return $_[0];
}
1;
#---------------
####
#----code.pl----
use warnings;
use strict;
use Sample;
my $var1;
my $var2 = tie( $var1, 'Sample', 'black', 'white' );
# The assigment test...
#$var1 = 'green';
#$var2 = ['green','red'];
# The overload test...
#$var1 + ' bishop';
#$var2 + ' bishop';
#print ref($var2),"\n";
print join( "\n", @{$var2->{data}} );
exit 0;
#---------------
####
Useless use of addition (+) in void context at code.pl line 28.
Argument " bishop" isn't numeric in addition (+) at code.pl line 28.