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
|
#!/usr/local/bin/perl
package myArray;
use Tie::Array ;
@ISA=qw/Tie::StdArray/ ;
use vars qw/$log/ ;
$log = 'log: ';
sub TIEARRAY {
my $class = shift;
my $p = shift || '';
#print "log $p ($log))\n";
$log .= "tie $p,";
return bless [], $class ;
}
sub STORE { my ($self, $idx, $value) = @_ ;
#print "storing $idx, $value ...\n";
$log .= "store $idx,";
$self->[$idx]=$value;
return $value;}
package myObj ;
use ExtUtils::testlib;
use Class::MakeMethods::Emulator::MethodMaker
get_set => [qw/a b c/] ;
sub new
{
my $class = shift;
bless { arg=> shift }, $class;
}
sub all { my $self = shift; return join (' ', values %{$self}) ;}
package X ;
use ExtUtils::testlib;
use Class::MakeMethods::Emulator::MethodMaker
object_tie_list =>
[
{
slot => 'a',
tie_array => ['myArray', "a"],
class => ['myObj', 'a_obj']
},
{
slot =>['b','c'],
tie_array => ['myArray', "bc"],
class => ['myObj', 'b_obj']
}
],
new => 'new';
package main;
use ExtUtils::testlib;
BEGIN { unshift @INC, ( $0 =~ /\A(.*?)[\w\.]+\z/ )[0] }
use Test;
use Data::Dumper ;
my $o = new X;
TEST { 1 };
# create a list of 2 object with default constructor arguments
TEST {$o->a(1,2)} ;
my @a = $o->a ;
# print Dumper \@a;
TEST {$o->a->[0]->all eq 'a_obj' };
TEST {$o->a->[1]->all eq 'a_obj' };
# verifie that tied array is used
TEST {$myArray::log eq 'log: tie a,store 0,store 1,'} ;
# create 2 object using constructor arguments
TEST {$o->b(['b1_obj'],['b2_obj'])} ;
TEST {$o->b->[0]->all eq 'b1_obj' };
TEST {$o->b->[1]->all eq 'b2_obj' };
# verifie that tied array is used
TEST {$myArray::log eq 'log: tie a,store 0,store 1,tie bc,store 0,store 1,'};
# create 2 object and assign them
my @objs = (myObj->new('c1_obj'), myObj->new('c2_obj'));
TEST {$o->c(@objs)} ;
TEST {$o->c->[0]->all eq 'c1_obj' };
TEST {$o->c->[1]->all eq 'c2_obj' };
exit 0;
|