Is there anything buried in the sea of Moose--or any object system on CPAN--that allows creating anonymous objects about as easily as creating anonymous hashrefs? I would be much happier using tiny objects to store structured data in my code, but the hashref syntax is just so darn convenient.
Here's an example of what I'd love to be able to do:
while (my $line = readline()) {
my ($ding, $dong) = parse($line);
next if $seen{$ding}++;
push @dingfirsts, Object::Anonymous->new(
ding => $ding,
dong => $dong,
line => $.,
);
}
for (@dingfirsts) {
say "First ", $_->ding, " on line ", $_->line;
}
Please ignore the obvious flaws in this contrived example. The most important part, to me, is that the object is defined by its constructor, and nowhere else. Methods would get created for any attributes given in the constructor.
Here's a quick implementation that may help explain what I'm after. There are some really obvious flaws that would prevent me from ever actually using this code--most significantly, it's modifying the Anonyject class at runtime, so it really isn't anonymous at all--but the mechanics are pretty close to what I'd want.
package Anonyject;
sub new {
my $name = shift;
my %args = @_;
my $self = {};
for my $arg (keys %args) {
# Store the given value
$self->{$arg} = $args{$arg};
# Create the accessor
my $methname = __PACKAGE__ . "::" . $arg;
*{$methname} = sub {
my ($innerself) = @_;
return $innerself->{$arg};
};
}
bless $self, $name;
}
package main;
use feature 'say';
my $obj1 = Anonyject->new(
ding => "wing",
dong => "wong",
line => 23_939,
);
my $obj2 = Anonyject->new(
something => "else",
);
say $obj1->ding; # Says "wing"
say $obj2->something; # Says "else"
say $obj1->something; # Should break, but doesn't
say $obj1->broken; # Breaks properly
So that's wishful thinking of the day. I'm hoping one of the popular object systems already does what I want, but maybe I can use Class::MOP to implement this properly. Any ideas would be lovely.
Class::MOP Makes This Easy (Score:1)
You should be able to do this in a few minutes with Class::MOP::Class [cpan.org], especially the
create_anon_classmethod.Badger::Class also makes this easy (Score:1)
The code sample you posted is almost there. You just need to create an anonymous package name instead of using
__PACKAGE__. I would simply append the accessor names to the base class, e.g.Object::Anonymous_ding_wong_linesub new {
my $class = shift;
my %args = @_;
my $self = { };
my $name = join('_', $class, keys %args);
for my $arg (keys %args) {
Re: (Score:1)
s/Auto/Anon/ # cut and paste failRe: (Score:1)
The code sample you posted is almost there. You just need to create an anonymous package name instead
Ha! Who woulda' thunk? I wrote that example code off-the-cuff in an attempt to better explain what I wanted. But that's a simple enough change that I'm now tempted to start using it as-is. Any other issues you can think might be lurking in the code?
Re: (Score:1)
I guess some sanity checks on the attribute names would be a good idea...
Data::AsObject (Score:1)
And also (Score:1)
* http://search.cpan.org/dist/Hash-AsObject/ [cpan.org]
* http://search.cpan.org/dist/Squatting/ [cpan.org]
And also:
* http://transfixedbutnotdead.com/2009/09/16/easy-anonymous-objects/ [transfixedbutnotdead.com]
As always there is more than one way|solution|module to do it
Re: (Score:1)
http://search.cpan.org/dist/Squatting/lib/Squatting/H.pm [cpan.org]