On Dec 20, 2007 2:01 PM, Tom Phoenix <[EMAIL PROTECTED]> wrote: > On 12/20/07, Andy Dixon <[EMAIL PROTECTED]> wrote: > > > sub test($) { > > Subroutine prototypes, like "($)" here, generally cause more harm than > good, alas. Most programmers should avoid using them in most cases. snip
You should not use prototypes until you have read and understood everything in FMTEYEWtKaPiP*, but it basically it boils down to this: prototypes are meant modify parsing behavior at compile time not ensure you have the right number or types of arguments. If you want to control the number and type of arguments (what most people try to use prototypes for), Perl provides plenty of tools to do so at runtime: #!/usr/local/ActivePerl-5.10/bin/perl use strict; use warnings; use feature ':5.10'; use Carp; my $n; say "expecting bad number of args:"; eval { $n = n_plus_one(1, 2) }; say $@ ? "got error [EMAIL PROTECTED]" : "\$n is $n\n"; say "expecting bad type"; eval { $n = n_plus_one(2.1) }; say $@ ? "got error [EMAIL PROTECTED]" : "\$n is $n\n"; say "this should work (and return 2):"; eval { $n = n_plus_one(1) }; say $@ ? "got error [EMAIL PROTECTED]" : "\$n is $n\n"; my @a = qw<a b c d e>; my @b = my @c = @a; say "expecting bad type:"; eval { $n = are_these_arrays_equal(@a, @b, @c) }; say $@ ? "got error [EMAIL PROTECTED]" : "\$n is $n\n"; say "expecting bad number of args:"; eval { $n = are_these_arrays_equal([EMAIL PROTECTED]) }; say $@ ? "got error [EMAIL PROTECTED]" : "\$n is $n\n"; say "this should work (and return 1)"; eval { $n = are_these_arrays_equal([EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED]) }; say $@ ? "got error [EMAIL PROTECTED]" : "\$n is $n\n"; sub n_plus_one { croak "bad number of arguments, expected 1 got " . @_ unless @_ == 1; my ($n) = @_; croak "the argument should be an integer, you passed [$n]" unless $n =~ /^[1-9][0-9]*$/; return $n + 1; } sub are_these_arrays_equal { croak "bad number of arguments, expected more than one and got " . @_ unless @_ > 1; croak "all arguments should be array references" if grep { ref ne 'ARRAY' } @_; my $one = shift; while (@_) { my $two = shift; return 0 unless @$one ~~ @$two; $one = $two; } return 1; } * http://library.n0i.net/programming/perl/articles/fm_prototypes/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/