in reply to Re: Perl High School
in thread Perl High School

my $q = new CGI;

Or rather:

my $q = CGI->new;

The indirect object notation will bite you at some point - please don't use it.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re: Re: (cLive ;-) Re: Perl High School
by dws (Chancellor) on Feb 22, 2002 at 07:31 UTC
    The indirect object notation will bite you at some point - please don't use it.

    Aw c'mon. People can get bit by indirect object syntax whether or not they use it intentionally. At some point, usually in the 10th hour of a codefest, a typo is going to trigger a cryptic  Can't locate object method "foo" via package "bar" error message.

    The only risk of using the syntax consistently is one might come to believe that new is special.

    If people stick with the new Package form, they're going to be just fine.

      Wrong. Look at the following code:
      package Foo; sub new { print "Right one!\n"; } 1; -------- #!/usr/local/bin/perl use Foo; # Insert 1000 lines of code here sub new { print "Wrong one!\n"; } # Insert 1000 lines of code here my $bar = new FOO; # Compare that with my $bar = FOO->new;
      By using indirect notation, you're removing the capability for the interpreter to spellcheck you if you have defined a new function in the current package. My example is contrived, but what if you're creating an object from within another object? That shows up every once in a while, right? :-)

      The only reasons I can ever see to use indirect notation are the following:

      1. You're a C++ programmer that doesn't want to really learn Perl
      2. You want to do that "cool thing" in Llama3 with no parentheses on method calls.
      (In case you can't tell, I find both those reasons ... lacking.)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Hmmm...
        package Gotcha; sub bar { print "Didn't think you would go here, huh?\n"; } package Foo; sub bar { print "This is what you probably thought would happen?\n"; } package main; sub Foo { "Gotcha"; } Foo->bar(); Foo::->bar();
        Unless you always slip that :: in, the main reason that the direct notation is safer than the indirect is that you are more likely to give different classes the same method names than you are to name a function and a class the same thing.

        That said, my aesthetic preference is for the direct notation.

        By using indirect notation, you're removing the capability for the interpreter to spellcheck you if you have defined a new function in the current package.

        I find this less than compelling, perhaps because I always use parenthesis on method or function calls.   my $bar = new FOO(); isn't going to be tripped up by having written a new routine in the current package. Consider

        package FOO; sub new { print "right!\n"; } package BAR; sub new { print "wrong!\n"; } my $f = new FOO(); # prints "right!"
        Update: hold on, my $f = new FOO;    # also prints "right!" Where's the problem?

(cLive ;-) Re: Perl High School
by cLive ;-) (Prior) on Feb 22, 2002 at 07:22 UTC
    dave - can you explain in a little more detail why not? No-ones pointed this out to me before...

    thanks

    cLive ;-)

    --
    seek(JOB,$$LA,0);

      For more details see this node (and for even more details, the pages quoted in that node from The Perl Cookbook and Object Oriented Perl).

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      Here's the (HTML-ized) warning from perlop. Note the word "exclusively" at the end:

      WARNING

      While indirect object syntax may well be appealing to English speakers and to C++ programmers, be not seduced! It suffers from two grave problems.

      The first problem is that an indirect object is limited to a name, a scalar variable, or a block, because it would have to do too much lookahead otherwise, just like any other postfix dereference in the language. (These are the same quirky rules as are used for the filehandle slot in functions like "print" and "printf".) This can lead to horribly confusing precedence problems, as in these next two lines:

      move $obj->{FIELD}; # probably wrong! move $ary[$i]; # probably wrong!

      Those actually parse as the very surprising:

      $obj->move->{FIELD}; # Well, lookee here $ary->move->[$i]; # Didn't expect this one, eh?

      Rather than what you might have expected:

      $obj->{FIELD}->move(); # You should be so lucky. $ary[$i]->move; # Yeah, sure.

      The left side of "->" is not so limited, because it's an infix operator, not a postfix operator.

      As if that weren't bad enough, think about this: Perl must guess (at compile time) whether name and move above are functions or methods. Usually Perl gets it right, but when it doesn't it, you get a function call compiled as a method, or vice versa. This can introduce subtle bugs that are hard to unravel. For example, calling a method "new" in indirect notation--as C++ programmers are so wont to do--can be miscompiled into a subroutine call if there's already a "new" function in scope. You'd end up calling the current package's "new" as a subroutine, rather than the desired class's method. The compiler tries to cheat by remembering bareword "require"s, but the grief if it messes up just isn't worth the years of debugging it would likely take you to to track such subtle bugs down.

      The infix arrow notation using "->" doesn't suffer from either of these disturbing ambiguities, so we recommend you use it exclusively.