Actually, what are prototypes in Perl? Why and where can they be needed? And how to use them correctly?

  • I correctly understand that this is also not a question, but an attempt to transfer here a couple of pages from publicly available textbooks? - PinkTux
  • 2
    @PinkTux "Please feel free to publish your notes or translations in the form of a" question with an answer "that you may have in the process of learning new technologies. I am sure the community will thank you for it!" - edem
  • @PinkTux is taken from here meta.ru.stackoverflow.com/questions/2983/… . What do you think admin rights about gratitude? - edem
  • I do not think that it is useful here, the more so formulated. In order for this question to be found a person should already know the word "prototype". And when a person knows this word Google immediately gives him the corresponding chapters of textbooks, where it is chewed. On SO, personally, I didn’t think to go for such information - Mike
  • one

2 answers 2

What are prototypes?

The prototype is a sample of the function arguments that is used by the compiler to verify the correctness of the passed parameters, as well as to change their context for the given function.

How can they be used?

They can be used immediately after the function name in brackets, for example: sub foo($$$) { ... } . And if the program includes support for use feature 'signatures' , then prototypes will need to be specified in the attribute :prototype() , since the brackets after the function name will be given to signatures.

The prototypes themselves are defined using a combination of several characters: $ ; @ + * & \[] $ ; @ + * & \[] . Read more about them here .

Why and where can they be needed?

The main purpose of prototypes is to allow user-defined functions to behave like those built into Perl. For example:

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

and the same function with the prototype will allow changing this behavior:

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

Those. it is as if this function (without a prototype) was called like this: foo(\@some_array) .

Of the interesting features, you can also mention the possibility of creating a new syntax when an anonymous function can be passed to a function without specifying the sub keyword.

 sub do_something_in_block (&) { print $_[0]->(); } do_something_in_block { my $a = 8; $a *= $a; }; 

    Here is a completely understandable article about prototypes: https://habrahabr.ru/company/regru/blog/232933/ , and not only about them.

    • 2
      Well, this is the answer-link! Answers, links, we are not welcome here. If you want to tell something on the topic, write a brief squeeze of the meaning of the article in the answer. - VladD
    • And how else can you answer the questions of the level "read my man"? For me, it is necessary to delete such questions without a vote, if they do not contain "man read and did not understand this ...". - PinkTux