in reply to Re: Why you shouldn't use the Indirect notation with AUTOLOAD
in thread Why you shouldn't use the Indirect notation with AUTOLOAD

Yet for some reason, indirect new calls have found their way into the documentation of many, many CPAN modules. I've seen code snippets that mixed direct and indirect new() calls depending on the examples in the docs.

There seems to be a persistent belief that new is a keyword. I don't know that anyone would every explain it that way, but indirect constructor code looks like people believe that it is a keyword, not a normal class method call.

Replies are listed 'Best First'.
Re^3: Why you shouldn't use the Indirect notation with AUTOLOAD
by TedYoung (Deacon) on Feb 28, 2005 at 18:58 UTC

    I always assumed that it was because of C, C++, Java, PHP, JavaScript/ECMAScript and VB. These languages (and more, I'm sure) all use new as a keyword. With so many languages constructing objects as new Foo it's no wonder why many do it in Perl. I look at it as yet another example of how perl's flexability allows people to program in a way comfortable to them.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
      Yet, that way provides for bugs, precisely because new isn't a keyword. Larry has said that he meant it as a migration aid, not as a syntax.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re^3: Why you shouldn't use the Indirect notation with AUTOLOAD
by Anonymous Monk on Mar 01, 2005 at 10:10 UTC
    Yet for some reason, indirect new calls have found their way into the documentation of many, many CPAN modules.
    Indeed, and that's very unfortunate. Let me try to give an explaination (I don't know whether that's the explaination, it's more a theory of mine):

    In the early years of perl5, new Module @args was the recommended way of calling a constructor. It's shorter than Module -> new (@args), and it's what C++ and some other languages use. So, CPAN modules, and also modules coming with perl5, used this syntax, and so did the main documentation.

    It wasn't that after a few years (1997 I think) that people like Tom and Mark-Jason noticed the potential problems, and started warning against its use. The documentation got updates, but there was still quite some code/documentation using the indirect object syntax out there. Which invites people to copy it when they write new syntax. So there has always been a large amount of module on CPAN using the indirect object syntax - and it has enough critical mass to make some new authors use this as well.

    Also, the problems with the indirect object syntax is infrequent enough that many people will never run into problems with indirect objects, even if they use it daily. How venerable you are to get bitten by the indirect object syntax also depends on your way of coding.

    There seems to be a persistent belief that new is a keyword.

    It's not, but sometimes I wonder whether it would have been better if it was.