Disclosure: This question refers to a couple of previous questions posted at SO.

I am writing a module for a Moose object intended to be serialized. The module contains a method which implementation depends on one of the attributes the object is initialized with.

One approach would be to write this method=d with an `if`, so each time the method is called it will decide which of the implementations it should follow. However, since after the object has been initialized, the method always works the same it seems to me it might be more elegant to make this decision only once, after initialization, and skip the if. It should be noted that the method I discuss is invoked intensively, so perhaps it would also be notably more efficient to skip the if.

This is t what I came up with:

package MyObj 0.001; use 5.012; use Moose; use namespace::autoclean; has 'length' => ( is => 'ro', isa => 'Int', required => 1, ); has 'is_circular' => ( is => 'ro', isa => 'Bool', required => 1, ); has '_offset_sub' => ( is => 'ro', isa => 'CodeRef', lazy => 1, builder => '_build_offset_sub', init_arg => undef, ); sub offset () { my ( $self, $current_coord, $offset ) = @_; return $self->_offset_sub()->( $self, $current_coord, $offset ); } sub _build_offset_sub { my ($self) = @_; if ( $self->is_circular() ) { # circular return sub { my ( $self, $current_coord, $offset ) = @_; return ( $current_coord - 1 + $self->length() + $offset ) +% $self->length() + 1; }; } else { # linear return sub { my ( $self, $current_coord, $offset ) = @_; return $current_coord + $offset; }; } } __PACKAGE__->meta->make_immutable; 1;
I kind'a like this solution, but the problem is it uses coderef, which makes the object more difficult to serialize. I usually use `Storable`, but it seems not to like coderefs.

I actually have two questions:

1. Do you find this coderef-using solution appropriate? Do you have any other ideas for coding the body of a method after object initialization? Perhaps this is all a huge overkill over a single `if`?

2. How would you recommend serializing Moose objects in general and this object in particular? I've been looking into `MooseX::Storage` but didn't find it very helpful. Specifically, it doesn't support coderefs.

Thanks,
Dave.


In reply to Creating a Moose object and serializing it by daverave

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.