Indirect object syntax, I hate you.

schwern on 2007-04-18T16:37:23

I hate indirect object syntax. I think its an unnecessary confusion just to support new Class. It doesn't really work for anything else as English sentence structure is subject then verb. Even new Class @args falls apart.

That's style, and style can be endlessly argued. This can't.

  use Test::More tests => 1;

sub Foo::bar { 23 }

ok Foo->isa("bar")

Can't locate object method "ok" via package "Foo" at -e line 1.


Grammatical ambiguity to support new Class. GRAH! -> is the second highest precedence operator and should take precedence over indirect object syntax. Worse, the meaning of ok Foo->isa("bar") changes whether or not the Foo symbol table has entries at compile time.


It's even more fun in DESTROY methods

Phred on 2007-04-18T17:42:03

A couple years ago I tracked down a bug in DBIx::Librarian where database handles were hanging around and causing trouble. Granted, the context I was using it in wasn't straight forward, but here's what the fix turned out to be:

sub DESTROY {
    my ($self) = @_;

-   disconnect $self if $self->is_connected;
+   $self->disconnect if $self->is_connected;
}

The author turned around a new release really fast, but it's a reminder to me to always use the direct syntax.

couldn't agree more

TeeJay on 2007-04-18T18:54:25

I've never used nor liked indirect object syntax, it is much less clear, and makes even less sense in non-english languages.

Doc Patches

chromatic on 2007-04-18T21:26:58

My favorite is reading code at Perl Monks that mixes different constructor invocation styles.

Are you up for patching the documentation of the most egregious offenders? I can think of a few places in the Perl core that need re-education.

problem is

educated_foo on 2007-04-18T23:37:26

## The problem is that you didn't use enough of it!

use Test::More tests => 1;

sub Foo::bar { 23 }
ok can Foo "bar"

The Fix

Ovid on 2007-04-18T23:55:21

As an aside for anyone reading this who didn't know it, you can fix this with a unary plus:

ok +Foo->isa("bar");

But yet, indirect object syntax is very, very annoying.

My only use of it

Alias on 2007-04-19T02:25:11

I hate ...

    new Object(...); ... but for reasons I don't really understand I love ...

    throw Exception(...);

throw me

jk2addict on 2007-04-19T13:11:14

I never use indirect syntax with one exception: exceptions:

throw My::Exception('Boom!');

I should really get our of that habit. Then the game would be complete.