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

In reply to Re^4: OO style: Placement of "new" (subtle) by tye
in thread OO style: Placement of "new" by crenz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.