On 8/30/07, Martin Barth <[EMAIL PROTECTED]> wrote:
> On Thu, 30 Aug 2007 15:39:14 +0100
> Andrew Curry <[EMAIL PROTECTED]> wrote:
>
> > That's rubbish,
>
> but you get a warning like:
>
> main::a() called too early to check prototype at -e line 1.
>
> Use Prototypes at the beginning of your file if you want to write the
>subs at the end.
snip

This would be a good point but for the fact that prototypes are
fundamentally broken* in Perl 5 and should not be used except in very
specific cases.

Here is a few things to consider before using prototypes:

#!/usr/bin/perl

use strict;
use warnings;

sub foo ($) {
        my $s = shift;
        print "$s\n";
}

my @a = qw<a b c d e>;

print "with a prototype\n";
#this not an error
#do you know why?
foo(@a);

my $sub = \&foo;

#this isn't an error either,
#but it gets different results
#do you know why?
$sub->(@a);

#this won't error
#for the same reason
&foo($a[0], $a[1], $a[2]);

#this is also not an error
#but it produces yet another result
#do you know why?
foo(@a[0 .. $#a]);

#lets see what happens without prototypes
print "without a prototype\n";
bar(@a);
$sub = \&bar;
$sub->(@a);
&bar($a[0], $a[1], $a[2]);
bar(@a[0 .. $#a]);

sub bar {
        my $s = shift;
        print "$s\n";
}


* well, this is a bit harsh, they just don't do what most people
expect them to do.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to