What's a prototype and how to use it?



  • What kind of prototype is Perl? Why and where could they be? How do you use them?



  • What's prototype?

    Prototype - is a model of the function used by the compiler to verify the correctness of the transferred parameters and to change their context for the function.

    How can they be used?

    They may be used immediately after the name of the function in parentheses, for example: sub foo($$$) { ... }♪ And if the programme includes support for signatures use feature 'signatures'the prototypes will have to be put in attribution. :prototype()as the functions will be assigned to the Signatories after the name of the brackets.

    The prototype itself is defined by a combination of several symbols: $ ; @ + * & []♪ Details can be read http://perldoc.perl.org/perlsub.html#Prototypes ♪

    Why and where could they be?

    The main purpose of prototypes is to enable users to act. It's like the Perl. For example:

    sub foo { ... }
    foo(@some_array); # в @_ - будет содержаться весь массив, т.е
                      # $_[0] = $some_array[0];
                      # $_[1] = $some_array[1];
                      # и т.д.
    

    and the same prototype function will change the behaviour:

    sub foo (+) { ... }
    foo(@some_array); # теперь в @_ будет содержаться только один элемент
                      # который является ссылкой на массив @some_array.
    

    I mean, it's like if this function (without prototype), it's called: foo(\@some_array)

    Of the interesting features, mention may also be made of the possibility of creating a new syntax when an anonymous function can be transferred without a key word sub.

    sub do_something_in_block (&) {
        print $_[0]->();
    }
    

    do_something_in_block { my $a = 8; $a *= $a; };


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2