What is Wrong With this Picture?

I’ve just found a number of subroutines defined this way in the code that I’m working on.

sub do_something () {
  my $parameter = shift;
  ...
}

I discovered the problem because I started getting errors about “too many parameters”. I knew what the problem was (the empty prototype) but it took a couple of minutes of head-scratching before I realised why it had been working before my changes.

Then I realised.

I have a dislike of “unnecessary” &s on subroutine calls. So almost without realising, I had removed them from the calls to these functions.

A few lessons have been learned.

Lesson 1 (for the original author of this code): Prototypes will trip you up. Do not use them.

Lesson 2 (for me): Ampersands aren’t always as pointless as they appear. They may be masking bugs in the code.

2 thoughts on “What is Wrong With this Picture?

Leave a Reply