Stories
Slash Boxes
Comments
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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
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 [cpan.org] 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!

        /I3az/

  • 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