KBD

Keith Devens .com

Thursday, November 20, 2008 Flag waving
You can have premature generalization as well as premature optimization. – Bjarne Stroustrup
← Convoluted Regular Expression of the DayMarkup Languages: Theory and Practice →

Daily link icon Thursday, August 7, 2003

New Perl class one-liner

You know, I never used to remember all the steps to creating a class in Perl. I always had to look it up, and it's one of those things you don't do very often. The "full" version is something like:

package Classname;
sub new{
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
}

Well, I realized there's an easy one-liner that's much easier to remember:

package Classname;
sub new{
    return bless {}, shift;
}

If you want to throw data in there, you can:

sub new{
    return bless {foo=>1,bar=>2}, shift;
}

Obviously if you want to call any methods on your object to initialize it you'll have to split that line up, but this gives you an easy-to-remember version that's simple to split up.

← Convoluted Regular Expression of the DayMarkup Languages: Theory and Practice →

Comments XML gif

Simon Willison (http://simon.incutio.com/) wrote:

It's still double-dutch to me. I never got the hang of OOP in Perl - I know it's really powerful, but the syntax is just ridiculously obscure. It's painfully obvious that OOP was "tacked on" to the language some time after the initial syntax was designed.

∴ Simon Willison | 7-Aug-2003 9:42am est | http://simon.incutio.com/ | #2654

Keith (http://www.keithdevens.com/) wrote:

Yep, Perl didn't get OO until version 5 I think. Or nested data structures, for that matter Smiley winking It'll be real nice in version 6 though.

Keith | 7-Aug-2003 9:44am est | http://www.keithdevens.com/ | #2655

George Schlossnagle wrote:

The real reason for the long form is to do:

package Classname;
sub new{
    my $self = shift;
    my $class = ref($self) || $self;
$self = {}; # or whatever you want it to be
    return bless $self, $class;
}

This way you can use new not only a a class method, but as a copy constructor as well.

∴ George Schlossnagle | 7-Aug-2003 10:01am est | #2656

M. Bean wrote:

I'm waiting for Perl 6 before I go back to even considering any further Perl development.

∴ M. Bean | 7-Aug-2003 3:36pm est | #2657

Keith (http://www.keithdevens.com/) wrote:

Besides simple scripts, I'm with you.

Keith | 7-Aug-2003 4:31pm est | http://www.keithdevens.com/ | #2658

Mean Dean (http://www.healyourchurchwebsite.com) wrote:

Actually, I've done some pretty gnarly things with PERL. Not like you can't shoot your foot clean off. But I don't go anywhere without having a copy installed. No like, the ActiveState Perl on one's PC is a very nice way to get small to medium jobs done ... whether it is slicing-n-dicing data, or getting content out on the Net.

∴ Mean Dean | 8-Aug-2003 1:44am est | http://www.healyourchurchwebsite.com | #2659

Sterling Hughes (http://www.edwardbear.org/blog) wrote:

Could be shortened to ::

package Classname;
sub new { bless {} }

∴ Sterling Hughes | 10-Aug-2003 7:26pm est | http://www.edwardbear.org/blog | #2670

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

November 2008
SunMonTueWedThuFriSat
 1
2345678
9101112131415
16171819202122
23242526272829
30 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

new⇒Calif. Supreme Court to take up gay marriage ban

I would argue the point is not​definitional.  While the word​marriage is su...

Justin: Nov 20, 4:37pm

Java join function

Meh, don't have null strings in​your string arrays imo, but you're​welcome ...

Keith: Nov 19, 7:51pm

Girls, please don't get breast implants

sorry but another thing i have to​make a comment on about you​men...the men...

happynow: Nov 17, 11:36pm

Books by Vincent Cheung

to all Cheung​fans:

read:

http://www.progin​osko.com/aquascum/cheung.h...

Zamir: Nov 16, 9:07am

Spider solitaire

To undo or not to undo that is the​question.
I'm an undoer. 
My dad​was n...

Can Turk: Nov 15, 2:50pm

Generated in about 0.241s.

(Used 8 db queries)

mobile phone