in reply to Re^2: OO style: Placement of "new" (easy)
in thread OO style: Placement of "new"

I simply moved package Foo to be after package Bar. That way Perl doesn't know that Foo is a package name ...

So as long as Perl knows that the target is package name (i.e., if you've gotten your uses and requires right, and haven't made a typo in the name), everything works as expected. Otherwise, you have a slightly harder time debugging. Is that about it?

  • Comment on Re: Re^2: OO style: Placement of "new" (easy)

Replies are listed 'Best First'.
Re^4: OO style: Placement of "new" (subtle)
by tye (Sage) on Apr 08, 2003 at 05:58 UTC

    Other than in the next couple of days, if I saw "undefined subroutine Bar::Foo called" and looked at the indicated code and saw "new Foo()", I'd be more likely to just switch to "Foo->new()" out of habit/principle than I would be to make sense of that error message and know exactly how to fix it some way other than making that change. So I don't think "slightly" is accurate for a lot of people.

    It is perfectly legal to compile code that constructs objects for which you have not yet loaded the module involved. You need to load the module before you run that code (well, technically, you don't, since you could have UNIVERSAL::AUTOLOAD load the module for you as a result of the constructor being called, but that'd just be asking for trouble... maybe), and it is traditional to load modules at compile time at the top source code that uses them. But there are good reasons to sometimes delay the loading of a module.

    If you had just happened to write your code in the order that I did and had avoided indirect object syntax, then the code would be perfectly valid and would work just fine. If you had written it in "my" order, used indirect object syntax, and so got the errors I did but hadn't just read a description of how I caused that error (since you didn't appear to know how to cause the error before that), then I think you could easily have had a tough time figure out what to do to fix it.

    I also find indirect object syntax just a little bit too ambiguous for the human mind. If you think of "new" as a special keyword (like it is in C++), then new Foo() makes sense and reads nicely. But thinking that is incorrect in the case of Perl and will likely cause you problems elsewhere.

    If you like it and you've never been bit, then feel free to keep using it, of course. I've seen other people bit (I don't even remember if I've been bit personally) and prefer to advise others to use safer techniques.

                    - tye
    A reply falls below the community's threshold of quality. You may see it by logging in.