Re: marine subroutine
- From: rvtol+news@xxxxxxxxxxxx (Dr.Ruud)
- Date: Fri, 31 Aug 2007 10:36:12 +0200
"Chas Owens" schreef:
Martin Barth:
Andrew Curry:
snipThat'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.
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.
This is nice content, but not presented well, so I wonder how many
people take the time to read and grok it.
Maybe you can transform it into an article, maybe both on this list and
on PerlMonks?
(and please give away the answers :)
--
Affijn, Ruud
"Gewoon is een tijger."
.
- Follow-Ups:
- Re: marine subroutine
- From: Chas Owens
- Re: marine subroutine
- References:
- RE: marine subroutine
- From: Andrew Curry
- Re: marine subroutine
- From: Martin Barth
- Re: marine subroutine
- From: Chas Owens
- RE: marine subroutine
- Prev by Date: Re: Logging STDERR and other output
- Next by Date: create a uft8 file
- Previous by thread: Re: marine subroutine
- Next by thread: Re: marine subroutine
- Index(es):
Relevant Pages
|
|