2011/09/28

Perl - Sentinel - version 0.02

I have just uploaded the first (non-development) release of Sentinel; version 0.02.

It provides a little helper function that creates lvalues, suitable for lvalue typed accessors that want to run real code on assignment (such as for type checking or coercion, or update triggers), rather than just update a scalar.
use Sentinel;

sub foo :lvalue
{
my $self = shift;
sentinel get => sub { return $self->get_foo },
set => sub { $self->set_foo( $_[0] ) };
}
It makes the following two lines equivalent
my $obj = Some::Object->new;

$obj->set_foo( 100 );
$obj->foo = 100;
This is rare among my modules, more or less the first thing I've written mostly as a result of ranting about it on #perl, rather than because I actually wanted it. My argument kept being that if anyone does want an lvalue accessor, it's trivially easy to write one given some function sentinel as in this example, and that actually implementing the sentinel function itself isn't hard; it's a small piece of obvious XS magic.

So here it is.

It has to cheat somewhat on versions of Perl before 5.14, because of the way lvalue context isn't properly as powerful as it now is. Long story short - there may be unit-test failures on some older versions of Perl; but I can't tell yet because the CPAN Testers web frontend is still down, so I can't see the smokers. If anyone sees any version-related failures, please do let me know.


Edit 2011/11/29: By the power of crowdsourced smoke testing, it appears this does work stably across many Perl versions - thanks all who've informed me.

5 comments:

  1. Awesome! Awe-Some! Thank you.

    ReplyDelete
  2. All tests pass on my 5.10.0 and 5.12.0 perls.

    ReplyDelete
  3. Excellent, nice to know it's working. rurban commented on my G+ post [*], that he's tested it as far back as 5.8 and all looks good. So I think we're done.

    *: https://plus.google.com/114433557481806433076/posts/jSBXX7y1TkK

    ReplyDelete
  4. Ah, it's not a 404 as such, it's just a limited post; so only people it was originally shared with can see it.
    And G+ doesn't have a way to open the sharing of a post once it's been created. Hrm... Sorry about that

    ReplyDelete