Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Reducing Perl OO boilerplate

by tilly (Archbishop)
on Feb 18, 2004 at 22:57 UTC ( [id://330085]=note: print w/replies, xml ) Need Help??


in reply to Reducing Perl OO boilerplate

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.

Replies are listed 'Best First'.
Re: Re: Reducing Perl OO boilerplate
by flyingmoose (Priest) on Feb 19, 2004 at 13:54 UTC
    Thanks, good input. This was all hand typed with a small font, so some of the typos were just that... but I'll take your advice on not needing a clone() and the hash efficiency. You are probably right about needing to do it by hand for a while first too. I guess I was just getting excited about wanting to play with my new toys. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://330085]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-25 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found