in reply to Re: Perl Module
in thread Perl Module

... 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

Replies are listed 'Best First'.
Re^3: Perl Module
by spx2 (Deacon) on Dec 31, 2008 at 06:54 UTC
    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 ?
      If you know how (Perl-)prototypes work, it is indeed predictable, but most think the Perl-prototypes work as do prototypes in languages such as C and then they are unpleasantly surprised. This really is one of the (few) dark corners of Perl IMHO.

      All parameter passing in and out of functions or subs in Perl is done by passing in or out a flat list. There is no standard mechanism to do what you describe, although I find the following template comes close:

      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

        There is a practical use - the representation returned is guaranteed to be true if the hash is populated and false otherwise. So you can test for whether a hash is empty very easily.

        I used to have a pedagogical use for this information. See Re: Re: Shift, Pop, Unshift and Push with Impunity! for code that on old versions of Perl would demonstrate Perl's hashes encountering a pathological case. But that no longer works because these days Perl decides whether to split when it encounters chains that are too long rather than when it needs a new bucket. (This is IMO a very reasonable choice.)

        An empty hash in scalar context gives 0, i.e. false. This is the only practical use of hash in scalar context I know of.
        I have never seen any practical use for this information.

        Practical use...

        sub next_power_of_two { my %s; @s{1..shift} = (); %s =~ '/'; $'; }

        ...for some obscure value of "practical" ;-)