in reply to Reducing Perl OO boilerplate

Some nits I need to pick with your code:

  1. In
    Package main:
    You have a syntax error. Change the ":" to a ";" and you have it made.
  2. Next, to get the effect you are after
    my $foo = HeckIfIKnow::new( -opt1 => '3456', -opt2 => '1234' );
    becomes
    my $foo = new HeckIfIKnow( -opt1 => '3456', -opt2 => '1234' );
As you have it written $foo looks like the following when printed via Data::Dumper's Dumper method:
$VAR1 = bless( { '3456' => '-opt2', '1234' => undef }, '-opt1' );
which I don't think you were after. After fixing it my way you get:
$VAR1 = bless( { '-opt2' => '1234', '-opt1' => '3456' }, 'HeckIfIKnow' );
which looks more like it.


Peter L. Berghold -- Unix Professional
Peter at Berghold dot Net
   Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.

Replies are listed 'Best First'.
Re: Re: Reducing Perl OO boilerplate
by exussum0 (Vicar) on Feb 18, 2004 at 03:24 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.

    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..
      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!