Here is my suggestion to improve Object::Tiny to allow for these features:
UPDATE: Added "Extra parameters" feature to callbacks; improved example.
use strict;
use warnings;
{package Object::Tiny;}
{package Foo;
use parent -norequire ,"Object::Tiny";
Object::Tiny::->import( qw|bar baz headers myhash|);
}
my $f = Foo::->new(bar=>44, baz=>"Value of baz");
$f->headers_set(["This is H-1","This is H-2"]); # SETTER - sets arrayr
+ef
print "Object f Scalar: $_=> ",$f->$_,";\n" for qw |baz bar |;
# Get a callback for each member of array ref:
$f->headers_forEach(sub{print "Object f :(Array component) header=$_[0
+];\n"});
$f->baz_forEach(sub{print "Object f : (Scalar accessed as array) baz v
+alue=$_[0];\n"});
$f->myhash_set({Uno=>"One", Dos=>"Two", Tres=>"Three"}); # Setter - se
+ts hashref
# Get a callback for each KEY.
# Extra parameters (R/W) returned are the extra param passed to forEac
+h()
$f->myhash_forEach(sub{print "Object f (HashRef): myhash KEY:$_[0]; VA
+LUE:$_[1]; Extra args($_[2],", ++$_[3],")\n"},
"Extra-arg1", 25);
(You can append THIS version of the module directly to the code above, to make it work)
BEGIN{
package Object::Tiny;
use strict 'vars', 'subs';
no strict 'refs';
our $VERSION = '1.10';
sub import {
return unless shift eq __PACKAGE__;
my $pkg = caller;
my $child = !! @{"${pkg}::ISA"};
eval join "\n",
"package $pkg;",
($child ? () : "\@${pkg}::ISA = __PACKAGE__;"),
map {
defined and ! ref and /^[^\W\d]\w*\z/s
or die "Invalid accessor name '$_'";
"sub $_ { return \$_[0]->{$_} };"
."sub ${_}_set { return \$_[0]->{$_} = \$_[1]
+};"
."sub ${_}_forEach { my (\$item,\$subref,\@pa
+rm)=(\$_[0]->{$_}, \$_[1],\@_[2..\$#_]);
my \$type=ref \$item;
\$subref->(\$item), retur
+n unless \$type;
if (\$type eq 'ARRAY')
{ \$subref->(\$_,\@par
+m) for \@\$item}
elsif(\$type eq 'HASH')
{\$subref->(\$_,\$item
+->{\$_},\@parm) for sort keys \%\$item};
};"
} @_;
die "Failed to generate $pkg:$@" if $@;
return 1;
}
sub new {
my $class = shift;
bless { @_ }, $class;
}
1;
}
OUTPUT:
$ perl test-obj-tiny.pl
Object f Scalar: baz=> Value of baz;
Object f Scalar: bar=> 44;
Object f :(Array component) header=This is H-1;
Object f :(Array component) header=This is H-2;
Object f : (Scalar accessed as array) baz value=Value of baz;
Object f (HashRef): myhash KEY:Dos; VALUE:Two; Extra args(Extra-arg1,2
+6)
Object f (HashRef): myhash KEY:Tres; VALUE:Three; Extra args(Extra-arg
+1,27)
Object f (HashRef): myhash KEY:Uno; VALUE:One; Extra args(Extra-arg1,2
+8)
I'm too lazy to document this, and propose the update to the module author (Never contributed to CPAN).
Please feel free to do this on my behalf.
"From there to here, from here to there, funny things are everywhere." -- Dr. Seuss