For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > prototype?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author prototype?
Budi Milis

2006-09-09, 3:57 am

Hi,

I saw some script on the net using prototype to define subroutines,
but most scripts are still not using it.
Is this a good practice/advisable (like use strict) and whats the benefits?

TIA.
John W. Krahn

2006-09-09, 3:57 am

Budi Milis wrote:
> Hi,


Hello,

> I saw some script on the net using prototype to define subroutines,
> but most scripts are still not using it.
> Is this a good practice/advisable (like use strict) and whats the benefits?


See: http://library.n0i.net/programming/.../fm_prototypes/


John
--
use Perl;
program
fulfillment
Randal L. Schwartz

2006-09-09, 6:58 pm


And without reading that article, the essence is

AVOID PROTOTYPES AT ALL COSTS

mmm.. ok?

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Paul Lalli

2006-09-09, 6:58 pm

Budi Milis wrote:
> I saw some script on the net using prototype to define subroutines,
> but most scripts are still not using it.


Correction. Most script-writers these days know enough NOT to use
them.

> Is this a good practice/advisable (like use strict)


No. It's a really, REALLY bad idea.

> and whats the benefits?


In theory, it provides compile-time checking of your function calls.
In practice, it doesn't work in at least half the cases, and will break
existing code if you put them in later.

For example, you want a subroutine which will take a scalar value and a
list of values, and determine whether or not that scalar is contained
in the list. So you write your function:

sub contained($@) {
my $value = shift;
my @list = @_;
for my $item (@list) {
if ($item eq $value) {
return 1;
}
}
return undef;
}


Now somewhere in your code, you have an array which contains the
searched for item as the first element:

my @item_and_list = ($search_for, $foo, $bar, $baz);
And you try calling your subroutine:
my $found = contained(@item_and_list);

The prototype on the subroutine will NOT reject this call. Instead, it
converts @item_and_list to a scalar, by passing the size of the array
rather than the values of the array. That takes care of the $ in the
prototype. The @ in the prototype is accepted because an empty list is
still a list. So instead of testing whether or not $search_for is
equal to any of $foo, $bar, or $baz, your subroutine will actually
check to see if the number 4 is contained in any empty list. You get
the wrong result back, without any notification from Perl that it
didn't do what you expected.

If you had omitted the prototype, this code would have worked fine.

Don't use prototypes. Ever.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com