|
my %TOTALS ;
sub import {
my ( $package , @names ) = @_ ;
my $caller = scalar caller ();
my $buffer = sprintf "\{ package %s;\n" , $caller ;
my $type = 0;
my $count = 0;
if ( ref $names [0]) {
my $ref = shift @names ;
if ( ref $ref eq "ARRAY" ) {
$type = 1;
$count = $ref ->[0] || 0;
} else {
die "huh?" ;
}
}
if ( $type == 1) {
foreach ( @names ) {
$buffer .= sprintf '
sub %s {
return ( defined $_ [1] ? $_ [0]->[ %d ] = $_ [1] : $_ [0]->[ %d ]);
}
', $_ , $count , $count ;
$count ++;
}
} else {
foreach ( @names ) {
$buffer .= sprintf '
sub %s {
return ( defined $_ [1] ? $_ [0]->{ "%s" } = $_ [1] : $_ [0]->{ "%s" });
}
', $_ , $_ , $_ ;
}
}
$buffer .= "\n\}\n" ;
eval $buffer ;
$TOTALS { $caller } = $count ;
die "yikes: $buffer" if $@;
}
sub get_count {
return $TOTALS { scalar caller ()};
}
1;
|