Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Reducing Perl OO boilerplate

by exussum0 (Vicar)
on Feb 18, 2004 at 03:24 UTC ( [id://329826]=note: print w/replies, xml ) Need Help??


in reply to Re: Reducing Perl OO boilerplate
in thread Reducing Perl OO boilerplate

I'll cut off the people who will tell you, don't use new, since if you have a function called new in your main:: space, it'll break your program.

BUT, tell me.. you have a function that's called add, it adds two things, right? You have a function called, parseString, it parses strings, right?

Why would you have a main:: funciton called new? New what? New time of day? New cheese? A function called new is badly named. :P

Update: See this node for more details on ->new vs new and someone actually having the problem.


Play that funky music white boy..

Replies are listed 'Best First'.
Re: Re: Re: Reducing Perl OO boilerplate
by Trimbach (Curate) on Feb 18, 2004 at 04:02 UTC
    I'll cut off the people who will tell you, don't use new, since if you have a function called new in your main:: space, it'll break your program.

    Uh, no. Although there is no rule that OO constructors be called "new" it's a pretty standard convention. Some modules (notably DBI) use an alternative constructor name, but it certainly does NOT break anything if you have a subroutine named "new" in package main.

    Why would you have a funciton called new? New what? New time of day? New cheese?
    It's a constructor. It makes a new object. That's why it's called new.

    As for the original question, I'm rather fond of Class::MethodMaker. Works for me, though YMMV.

    Gary Blackburn
    Trained Killer

      Nononon.. you missed the point. If I have a program that uses class Foo, and in my program somewhere else, i declare sub new, doing new Foo will break. It's not "why is the constructor called new" but if you do "new Foo" and new() exists on the same scope of your program. Take this for example:
      use Foo; sub new(*) { die "wuh?"; } $x = new Foo();
      With a package..
      package Foo; sub new { return bless { a => 1 }, shift; } sub new2 { return bless { a => 1 }, shift; } 1;
      new2 works, new breaks. THAT is my point. Why would anyone do this, I don't know. But people have pointed out that ->new is the better convention.

      So before you berate me more on what a constructor is, i suggest you reread my posts. Thank you.


      Play that funky music white boy..
      Class::MethodMaker appears to be exactly what I want in terms of defining data. Good find. I need to still do some exploring to find what I can do in terms of constructor cleanup (so that I don't need to call object methods to set all of the variables after 'new' is invoked .. aka constructors that take parameters like they are supposed to), but this will help with method cleanup quite a bit. Thanks!
        I've had to do exactly that with Class::MethodMaker. I needed to initialize an object with a LOT of data (and a LOT of method calls) and initializing the object through repeated method calls was S-L-O-W. So, I subverted the regular constructor with my own:
        package Datawarehouse::Employee; use Class::MethodMaker new_with_init => 'new'; sub init { # ...Load up a whole lot of data # Bless my own object into the class Datawarehouse::Employee->new_new(\%args); } sub new_new { # This constructor is a HELL of alot faster than creating an # object by calling accessor methods one by one. my $class = shift; my $args = shift; my $obj = bless $args, $class; # Loading the whole hash as one }

        If you do this while using the key_attrib parameter in Class::MethodMaker you'll need to call the key_attrib method explicitly so that your hand-made object is tracked like the regular one's are. Looking at the Class::MethodMaker source code is a great way of figuring out exactly what's going on with the autogenerated methods.

        Gary Blackburn
        Trained Killer

Log In?
Username:
Password:

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

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

    No recent polls found