Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^3: Golf: overtone calculator

by tadman (Prior)
on Sep 02, 2002 at 13:26 UTC ( #194573=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Golf: overtone calculator
in thread Golf: overtone calculator

They are poison. You should only use them when you have to, like forcing an array or subroutine reference. Here's two typical examples:
sub my_grep(&) { ... } sub my_pop(\@) { ... }
Putting in scalars (i.e. ($$)) is asking for trouble since it converts any arrays to scalars automatically. I found this really quite worrying, and you can see my brief rant in Function Prototypes and Array vs. List (Pt. 2).

This all comes from a technique of jamming arrays into functions, which I do all the time to be efficient. Things like this:
my $sth = $dbh->prepare("SELECT id,name,age FROM foo"); $sth->execute(); while (my $row = $sth->fetchrow()) { $self->some_function(@$row); }
Now, if you've prototyped your function, you're going to get the number 3 every time. This is put in to the 'id' field, so you might actually think it's valid data, too.

Instead, just leave those things off. That's why merlyn says "...and this is why we tell people DO NOT USE PROTOTYPES".

Replies are listed 'Best First'.
Re: Re^3: Golf: overtone calculator
by theorbtwo (Prior) on Sep 02, 2002 at 23:55 UTC

    Nice rant, tadman; well-explained... except for one minor point: in your example, the prototype doesn't apply, because the function is being called as a method... I think. I remember reading that, somewhere, but rereading the relevant part of perlsub it only mentions functions called with a & or via a subref.

    Confession: It does an Immortal Body good.

      Your memory is good theorbtwo++ for spotting that.

      The second paragraph of perlsub docs for 5.6.1 reads...

      Method calls are not influenced by prototypes either, because the function to be called is indeterminate at compile time, since the exact code called depends on inheritance.

      I wonder if this means things have changed for 5.8 (your link points there)?

      Well It's better than the Abottoire, but Yorkshire!
      You know, that could explain why, with the code I'm working on, I keep finding invalid prototype definitions for things and yet there's no associated warnings. Things like this:
      # Five in prototype... sub foo($$$$$) { my ($self, $foo, $bar, $baz, $bim, $bam) = @_; # Six in list # ... }
      Things like this are routinely called, and yet, never and warnings. Thanks for the heads up.

      It's good to know that prototypes are useless in that context.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://194573]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-05-27 00:02 GMT
Find Nodes?
    Voting Booth?