12.1.7 Parameter Profile of Perl Functions
12.1.7 Parameter Profile of Perl Functions
The parameter profile of a subprogram is the number, order and types of its formal parameters. In the case of Perl, the parameter profile, for regular subprograms, is very simple. A regular Perl subprogram does not have any individual formal parameters; strictly speaking, it has only one formal parameter: @_,which is an undistinguished list of scalars of arbitrary length. This single formal parameter is a list of individual “formal parameters.” The body of the subprogram has to fish out the scalars from this homogeneous list and make sure the appropriate number of parameters has been passed. The manner in which it culls elements from the list gives it the “actual parameters” in the proper order. The elements are all scalars. There are good points as well as drawbacks to this scheme.
Some of the good points are given below.
• The model of parameter passing is simple and simplicity has its virtue. Subprograms are easy to write. This adds to the fact that Perl is a language that is good for rapid prototyping.
• Perl allows one to write functions that take an arbitrary number of parameters. This is of great value in certain contexts. Such functions can be quite powerful.
• Perl allows type checking of parameters for those who want it. We will see that this is done using the concept of prototypes.
Some of the drawbacks of Perl functions are given below.
• The only formal parameter is an undifferentiated list. The subprogram code has to obtain the specific elements. This adds code to the body of the subprogram. It is not really a major inconvenience because the code required is usually minimal and the programmer who writes the subprogram knows the order of the parameters. But, it may make updating of code difficult unless the programmer says what the “formal parameters” are through good commenting or good coding.
• This makes it difficult to pass several lists or hashes unless there is some way to indicate the lengths of the lists or hashes. However, we can do so by passing parameter references.
• Since simple Perl functions do not check types of “actual parameters,” it is possible to make errors, especially by those who are used to strongly-typed languages like Pascal or C. Some simple type checking can be done using prototypes.
