Enlightened Monks!

I have a class with a few attributes, and most of the time, I'm creating a new object of this class with the same, predefined set of values. Let's say that the object looks like this:

package Attack { use Moo; use Types::Standard qw( Str Int ); has type => ( is => 'ro', isa => Str ); has damage => ( is => 'ro', isa => Int ); has speed => ( is => 'ro', isa => Int ); }

While there are times, when I need a custom attack, I use three basic attack objects almost all of the time: light, fast and heavy. I want the code to reflect this by providing an easy way to get each of those three objects. I could do it by providing custom constructor methods:

sub light { my $self = shift; return $self->new( type => 'light', damage => '1', speed => '5', ); } sub fast { my $self = shift; return $self->new( type => 'light', damage => '1', speed => '7', ); } sub heavy { my $self = shift; return $self->new( type => 'heavy', damage => '2', speed => '4', ); }

And it works. But I could also do it by processing the constructor args. By overriding the BUILDARGS method I could create shorthand to call Attack->new() with a single argument. This would look like this:

sub BUILDARGS { my ($self, @args) = @_; if ($args[0] eq 'heavy') { return { type => 'heavy', damage => '2', speed => '4', }; } # same for 'light' and 'fast' of course else { return { @args }; } }

Using the first approach, I would create an object like this:

my $att1 = Attack->heavy;

With the second:

my $att2 = Attack->new('heavy');

Of course, both $att1 and $att2 are identical, I'm just wondering about the style. As you probably figured out from the example, it's nothing serious, it's more of a weekend project, and I'm much more interested in making it work in an elegant way than in making it work at all. The first way seems shorter, and (to me) looks better when called, the second makes it very clear that I'm calling a constructor.

What do you think? What would be your preference, and why?

- Luke


In reply to Convenient Constructors - a Moo question by blindluke

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.