Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Friday February 20, 2009
10:04 AM


[ #38517 ]

We've started to use Moose pretty heavily at work, but until today, I've not had the chance to write any Moose classes myself. Today, I wrote my first two Moose classes:

package PIPs::QueryParams::Param;

use Moose;

has name => (is => 'ro', isa => 'Str', required => 1);

has method =>
        (is => 'ro', isa => 'Str', lazy => 1, default => sub { $_[0]->name });
has required   => (is => 'ro', isa => 'Bool', default => 0);
has repeatable => (is => 'ro', isa => 'Bool', default => 0);
has datatype   => (is => 'ro', isa => 'Str',  default => 'xsd:string');
has rel        => (is => 'ro', isa => 'Str',  default => '');

no Moose;


And ...

package PIPs::QueryParams;

use Moose;
use Carp ();
use PIPs::QueryParams::Param;

has params => (
    is  => 'ro',
    isa => 'ArrayRef[PIPs::QueryParams::Param]',

    my ($class, %args) = @_;
    my @params;
    my $controller           = $args{controller};
    my $allowed_query_params = $controller->allowed_query_params;
    foreach my $name ( sort keys  %$allowed_query_params) {
        my $args = $allowed_query_params->{$name};
        $args->{name} = $name;
        push @params => PIPs::QueryParams::Param->new(%$args);
        params => \@params,

no Moose;


And I can now just do this:

my $params = PIPs::QueryParams->new( controller => $controller );
my $list   = $params->params;
say $_->name foreach @$list;

I'm sure those can be written better, but those were soooo much easier to write than typical Perl classes. Ultimately, this is going to be used to auto-generate documentation for our REST API.

And note, because of the annoying sort of guy that I am (and preferring immutable objects), everything is read-only.

Update: Fixed the code per autarch's suggestion below. His comment now won't make much sense, but I didn't want an example of bad Moose code here.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • .... why the trailing "no Moose"?

    • It removed the functions that Moose exports into your package. It would be annoying if someone tried to call $object->has :)

    • The lovely MooseX::Declare [] lets you avoid even that (and it's an incredible module), but unfortunately, it's very alpha and has plenty of bugs. Still, look what it buys you:

      use MooseX::Declare;

      class BankAccount {
          has 'balance' => ( isa => 'Num', is => 'rw', default => 0 );

          method deposit (Num $amount) {
              $self->balance( $self->balance + $amount );

          method withdraw (Num $amount) {

      • > I can't wait until that's production ready.

        Ohhh. Shiny! :-)

      • All MooseX::Declare does to not require you to unimport all Moose keywords (and possibly other imports) is

        use namespace::clean -except => 'meta';

        to remove all previously defined and imported functions (except for the 'meta' method Moose gives you) at the end of compile time. No need to wait for anything.

      • I can't wait until that's production ready.

        I think everyone who left the London Perl tech talk last night felt & thought the exactly same thing.

        Patience is a virtue!


  • I don't understand the bit about the final '' in your comment. I think a better way to do that is make method lazy and have the default be:

    sub { $_[0]->name }

    • The reason for the final '' is that it put a string in the method, thus ensuring it would pass type validation. Your solution is much nicer. Thanks :)

  • So, in the interest of TIMTOWTDI, here is another approach that uses coercion to extract the params list from the controller object.

    package PIPs::QueryParams;
    use Moose;
    use Moose::Util::TypeContraints;
    use PIPs::QueryParams::Param;

    # I am assuming this is the name
    # of your controller class and
    # it is a non-Moose class, if it is
    # a Moose class, you can remove this
    # (assuming the class is already loaded)
    # because Moose does this for you
    class_type 'PIPs::Controller';

    subtype 'PIPs::ListOfParams'

  • Thinking about this example, there's a code smell in general with your use of BUILDARGS.

    BUILDARGS is really _not_ for munging arguments, it's there to allow for a constructor which doesn't take named argument, like

    my $user = User->new($user_id);

    If you're using it to add extra arguments, consider using lazy default/builder, per my suggestion. If you're using it to coerce, use a coercion per Stevan's example.

    • OK, I'll buy the code smell argument because you have more experience with this than I do, but I don't understand. I like the lazy/default example, but Stevan's example is longer than mine and doesn't seem to buy me anything. Plus, as Stevan pointed out, it has the drawback of an apparently misnamed constructor pair. What am I missing?

      • The advantage of coercion is exactly what Stevan says, it lets you reuse that bit of logic across classes.

        The name mismatch is weird, but I'd probably just have people call the constructor like this:

        PIPs::QueryParams->new( params => $controller->allowed_query_params );

        I honestly don't like _any_ of these options much myself, but I don't understand your APIs or problem domain well enough to come up with a better solution.

        There's just something about using BUILDARGS to do what coercion can do that st