Hacks Archives

Anonymous Classes With Private Data

A long while back (I’ll find the reference if I can) Stevan Little, author of Moose, commented that part of what he wanted for a p5mop was the ability to have truly private data in classes. Much in the way Perl 6 has $!data attributes that are simply private data, he wanted to just be able to use Perl’s regular variables in this same way.

I took this as a bit of a challenge and several iterations later, I had a working system. I then spent months trying to decide if I wanted to put it on CPAN. I kept weighing utility vs practicality. Though it is an interesting thought exercise, I have no idea if its a good idea.

A few things happened which made me soften my view. Most importantly, the great Damian Conway released Dios which does bring lots more of the Perl6 style classes to Perl5. This lead me to stop worrying that people would actually try to use my module for real heavy lifting; if you need that use Dios. Also Stevan gave a talk at YAPC::NA which showed an exciting and I think very promising reimagining of the p5mop project.

With these two projects out there mine can just be a curiosity. I kept finding myself showing it off and wondering. Finally, today Yanick Champoux found himself pondering blessed-subref-based objects and I reminded him of my module, which I had shown him at YAPC. I mentioned that I still was on the fence about releasing it to CPAN, he replied:

So I did.

I’m happy — and a little scared — to introduce Class:Anonymous to the CPAN. It may be the strangest thing I’ve put there yet.

You have nothing to lose but your chains!

Perhaps a misleading title. Seeing as this is not a political blog but a Perl one, I’m going to talk about method chaining, not worker’s unions.

Method chaining is the practice of consecutively calling methods on the return of a previous method. This comes in primarily two flavors. The first isn’t as common in Perl, though it is used extensively in Mojolicious, is when a method has nothing useful to return, it can return itself. This allows for say chaining setter methods $self->set_foo("FOO")->set_bar("baz"), or chaining related test methods

my $t = Test::Mojo->new;
$t->get_ok('/page/1/')
   ->status_is(200)
   ->text_like('#id' => qr/foo/);

While this is useful, it’s not my topic today. I’m going to talk about the more simple form, calling a method that returns an object, then calling a method on it, and so on.

Reflecting on the DFW.pm winter hackathon competion

I know it has been a while, but I’m finally getting around to posting my reflections on the Dallas-Ft. Worth Perl Mongers Winter Hackathon. Better late than never right? I will talk about my solution, but I also want to comment on the hackathon itself, which I thought was an excellent bit of fun.

About Joel Berger

user-pic As I delve into the deeper Perl magic I like to share what I can.