in reply to "Indirect" object syntax?

I'll offer another linguistic interpretation for you and call this heuristically ambiguous object syntax (in addition to Perldoc's own references to indirect as well.)

The reason for this is spelled out in the perlobj documentation, which recommends the so-called indirect object syntax be avoided.

Consider the code below, which provides both a new and connect constructor.

The instantiation of $obj4 breaks because connect is also a built-in command, and Perl's heuristics prefer it over the connect constructor of the Example class. The same is true for the popular DBI->connect() constructor, as well as many other real-world examples you find in the wild.

It's best not to let something as muddy as heuristics determine what your code does, so I personally avoid such constructs with a passion. Leave that stuff to Java where it belongs ;).

use strict; use warnings; my $obj1 = Example->new(); my $obj2 = new Example; my $obj3 = Example->connect(); my $obj4 = connect Example; package Example; # Constructors # (be subclass friendly, even in example code ;) sub new { my $class = shift; $class = ref($class) || $class; # subclass boilerplate. return bless { }, $class; } sub connect { my $class = shift; $class = ref($class) || $class; # subclass boilerplate. return bless { }, $class; }

Replies are listed 'Best First'.
Re^2: "Indirect" object syntax?
by muba (Priest) on Nov 24, 2015 at 00:55 UTC

    Apero, although I do thank you for chiming in and demonstrating why using the thing called "indirect object syntax" isn't a good idea, my question wasn't about that. I am (almost) fully aware of the drawbacks of the syntax. My question was more about whether —from a linguistical point of view— the name "indirect object" was the most accurate.

    Again, I'd like to use the kick $ball; # i.e. $ball->kick example, where $ball obviously is a direct object and not an indirect object.

    But Athanasius nicely quoted the friendly manual and made me realize that, although Perl does resemble English, Perl simply isn't English.

      My question was more about whether —from a linguistical point of view— the name "indirect object" was the most accurate.

      I figured, although backing up my alternative naming convention (which I'll hardly claim as the "best", or even better than indirect) with reasoning might help another reader realize what's going on behind the scenes when making Perl English readable; that's not always a useful metric as you noted.

      The discussion is interesting since many of Perl's constructs can be read in an English-like way (even promoted as options at times, as in use English. On the other hand, I frequently find lines, especially long unbroken ones, hard to read.

      Some more musing (or rambling, depending on your opinion) on subject/verb ordering in Perl follows..

      I didn't mean to take away from your linguistic pondering too much (although hopefully another reader can benefit from that.) In any event, thanks for the discussion!

        Ah, well then. I must've misunderstood the intention behind your post. My bad.

        do('many', 'things', 'now') if ( $ready and $set and $prepared );

        could be made even more English-like (Englishy? Englishesque?) if we ignore for a moment that do is a built-in function ;)

        do 'many', 'things', 'now' if $ready and $set and $prepared;

        And even you prefer the if-this-then-than notation over the that-if-this syntax, there are neat ways:

        $ready and $set and $prepared and do 'many', 'things', 'now'; $ready and $set and $prepared? do 'many', 'things', 'now' :() ;

        What's funny about that last one is that it's... well... rather Englishy indeed:

        (Are you) ready and set and prepared? Do many things now <shocked emoticon>;

        Coincidentally (or maybe not, as it's neatly linked to on my homenode), I've asked about the various ways to write conditionals before.