... and are best left alone, unless you know what you are doing.One thing they DO NOT do is compile-time (or even run-time) checking of the arguments in the argument-list. Consider the following: sub test_noproto {print shift;};
sub test_proto ($) {print shift;};
$scalar = 'A';
@array = qw /X Y Z/;
%hash = ('key', 'value');
test_noproto($scalar); # gives as expected: A
test_noproto(@array); # gives as expected: X
test_noproto(%hash); # gives as expected: key
test_proto($scalar); # gives as expected: A
test_proto(@array); # gives: 3 Did you expect that?
test_proto(%hash); # gives: 1/8 Did you expect that?
In some way (but not in others) one can say that the $ prototype magically causes the argument to the sub to be evaluated in scalar context. This all happens silently without any warning or error to the user.
Saying (as perlsub does) "Perl supports a very limited kind of compile-time argument checking using function prototyping." is misleading to the unwary.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |
actually test_proto(@array); was predictable , it's test_proto(%hash); that was not , is there any explanation of "1/8" value or is it just sci-fi ? also,is there any way of using parameters to functions as you do in C or C++ ? I mean you just write down the prototype and the argument names are implicitly taken to be variables which have the scope of the function to which they are arguments of ?
| [reply] [d/l] [select] |
sub my_subroutine {
my ($required_parameter_1, $required_paramater_2, @optional_parame
+ters) = @_;
}
Of course you would use more meaningful variable names in a real situation.The '1/8' is the value of a hash in a scalar context. It means '1 bin out of 8 possible bins are being used'. 'Bins' are what is being used internally by a hash to store the keys and values. I have never seen any practical use for this information.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] |
I would describe it rather differently. I would say that prototypes give people new ways of creating bugs while being mislead into thinking that they are doing something that is potentially good. | [reply] |