I'm amazed that merlyn didn't pipe up in this thread. :-)

Your ref construction is the one he disliked at ref($proto) - just say no!. (Further explanation available at A Cat's eye view of OO.) Unless you really think that you will be using new as a clone() method, you can drop that one. Furthermore, while you are at it, the usual way that new will be called is with the package name (aka class) as the first argument. Calling it $obj is misleading at best. Furthermore if you have gone to the work of creating a hash, why dereference it and build another to take a reference to that? You had a perfectly good hash.

With those changes your code turns into:

package HeckIfIKnow; sub new { my ($class, %opts) = @_; return bless(\%opts, $class); }
Now for where you call it. The keyword is package, not Package. You need a ;, not a :. You have to make it a method call, not a function call. And given that you don't have to tell data from metadata, there is no need to make all of your options have - in the name (and it is customary not to). With that in mind the rest of your code becomes:
package main; my $foo = HeckIfIKnow->new( opt1 => '3456', opt2 => '1234', );
Yes, you can reduce the boilerplate further than this with a variety of modules. But my advice is that until you are comfortable with the mechanics of how Perl does things, that you put off piling on shortcuts and complications.

In reply to Re: Reducing Perl OO boilerplate by tilly
in thread Reducing Perl OO boilerplate by flyingmoose

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.