Re: problams with IO::Socket::INET

Mark Mielke (markm@nortel.ca)
28 Jan 1998 00:37 EST


In message "Re: problams with IO::Socket::INET", 
'tchrist@jhereg.perl.com' writes:
>>Tom Christiansen wrote:
>>> >I dont use the 'X->new()' syntax; I use 'new X()'.
>>> That's a mistake, although the docs would lead you to
>>> believe otherwise.  That's mostly my fault.  I know
>>> better now.  Indirect object notation is flawed.
>>Why is it a mistake, I too use the new X() form
>I intend to show, using three demonstrations, that because the resolution
>of parser ambiguities of method calls versus function calls lead to
>confusion and broken programs, and that because of this, indirect objects
>should not be used.
>+--------------------+
>| DEMONSTRATION ONE: |
>+--------------------+
>Consider the following:
>    $a = meth $x->{whatnot}

This is why it's:
     $object = new X();
Not:
     $object = new X;

No ambiguity in my mind. Unless the parser somehow ignores the ()?

>+--------------------+
>| DEMONSTRATION TWO: |
>+--------------------+
>Because the syntax 
>    fn X;
>does not always mean
>    X->fn();
>Sometimes it means
>    fn(X);

In general i choose not to use the list syntax for this very reason.
Ambiguity. (all my calls usually LOOK LIKE "fn(X)", not "fn x")

>When is it one, when is it the other?  Do you know how the compiler
>guesses this one?  It looks to see at compile time, whether there is a
>package X in existence.  And of course, if X is a variable, you lose.
>And the way this is determines depends upon load order!

I don't let the compiler guess. If you've ever seen my code i'll WRITE:

    $seconds = ($hours * 3600) + ($minutes * 60) + $seconds;

No ambiguity. Good programming practice... remember?

>We call this strange magic at a distance.  

And i don't like magic so i avoid it.

>Compare this:
>    package Mine;
>    sub new {}
>    $x = new FileHandle;  # not a method call: Mine::new('FileHandle');
[ repeating this three times - deleted ]

You're doing it again! Trying to use magic, and then wondering about it?

      $x = new FileHandle();

NO ambiguities.

>Going back to the first demo, what does this do:
>    package Mine;
>    sub methA {} 
>    sub methB {
>	my $self = shift;
>	$answer = methA $self->{FIELD};
>    }

Ack! Stop using magic.
That's your choice not mine.

>All because of ordering!  Can you believe that?
>Are you afraid yet?

There are quite a few other situations that could be concucted about perl
syntax being dangerous that DON'T include this one.

>+----------------------+
>| DEMONSTRATION THREE: |
>+----------------------+
>This is going to a bit more complicated, but it's not 
>uncommon, and its subtlety will make you scream.
>I'll include only the important parts.
>File Alpha.pm
>    package Alpha;
>    sub new { bless {} => shift }
>    sub clone {
>	my $self = shift;
>	my $type = ref $self;
>	my $copy = new $type;

ERROR! I'm not even going to read anymore. If you continue to ignore good
programming practices you're screwed. If you want i could compile a list of
newbie errors and then present them as reasons for why the syntax in perl
is wrong. Anybody who chooses to use ambiguous code is stupid.

   my $copy = new $type();

Get it right.

mark

--                                                  _________________________
.  .  _  ._  . .   .__    .  . ._. .__ .   . . .__  | Northern Telecom Ltd. |
|\/| |_| |_| |/    |_     |\/|  |  |_  |   |/  |_   | Box 3511, Station 'C' |
|  | | | | \ | \   |__ .  |  | .|. |__ |__ | \ |__  | Ottawa, ON    K1Y 4H7 |
  markm@nortel.ca  /  al278@freenet.carleton.ca     |_______________________|