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


In reply to Re: Setting accessor with Object::Tiny by NetWallah
in thread Setting accessor with Object::Tiny by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.