sub get_props {
my $obj = shift;
# return just a list of values
return ( map { $obj->$_ } @_ );
# return a list of key/value pairs
return ( map { $_, $obj->$_ } @_ );
# to return references, just wrap the previous examples
# into [] or {} as appropriate
}
my @meths = map { "get_value$_" } 1..3; # or whatever your methods are really
print "Values: @{[get_props($obj, @meths)]}\n"; # tweak to fit get_props()
####
package Object;
use Hash::Util::FieldHash;
use overload '%{}' => \&_get_props, fallback => 1;
Hash::Util::FieldHash::fieldhash my %objects;
sub new {
my $class = shift;
my $self = bless do { \my($x) }, $class;
$objects{$self} = {
prop1 => 'foo',
prop2 => 'bar',
blorf => sub {time},
};
bless $self, $class;
}
# getters/setters...
sub get_prop1 {
$objects{shift(@_)}->{prop1};
}
# ... and so on
sub _get_props {
my $obj = shift;
$objects{$obj};
+{
map {
$_,
ref $objects{$obj}->{$_} eq 'CODE'
? $objects{$obj}->{$_}->()
: $objects{$obj}->{$_}
} keys %{$objects{$obj}}
}
}
1;
__END__
####
use Object;
my $foo = Object->new;
print "Values: ", join(", ", @$foo{qw(prop1 blorf)}),"\n";
__END__
Values: foo, 1492814598