in reply to Re^2: Perl Module
in thread Perl Module

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 ?

Replies are listed 'Best First'.
Re^4: Perl Module
by CountZero (Bishop) on Dec 31, 2008 at 07:16 UTC
    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" ;-)

        LOL TIMTOWTDI!!

        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