|Problems? Is your data what you think it is?|
Split does not behave like a subroutineby bojinlund (Prior)
|on Jul 18, 2020 at 06:00 UTC||Need Help??|
I have tried to understand the documentation of the Perl function split. Doing this I have realized that after more that 30 years usage of Perl I have still not understood some basic properties of Perl. Here follows some of "my new findings" and questions.
From Perl documentation:
The Perl model for function call and return values is simple: all functions are passed as parameters one single FLAT LIST of scalars, and all functions likewise return to their caller one single FLAT LIST of scalars.
A LIST value is an unnamed list of temporary scalar values that may be passed around within a program from any list-generating function to any function or construct that provides a list context.
LIST, list value constructor and list value
A list value can be created by using a list value constructor. It is a number of arguments separated by Comma Operators.
"In list context, it's just the list argument separator, and inserts both its arguments into the list. These arguments are evaluated from left to right."
A Perl list (= list value) is a sequence of scalar values. A list is not a scalar. Not even the empty list is a scalar.
This means that all list values are flat!?
Probably is the flattening a part of the comma operator and done for all lists (= list value)
"The null list is represented by (). Interpolating it in a list has no effect. Thus ((),(),()) is equivalent to (). Similarly, interpolating an array with no elements is the same as if no array had been interpolated at that point."
Positional arguments and parameters
It took me a long time to understand that my problem was, a subroutine returning an empty list, in the parameter list in the call to a subroutine.
Here I use: In a subroutine call the arguments are passed/bound to the parameters (formal argument) in the subroutine definition. The arguments for a call are evaluated, and the resulting values are passed to the corresponding parameters.
I have always though, that the commas in a call to a subroutine, separate the argument list in sequence of values each corresponding to a parameter. call('P1', 'P2', 'P3').
There can be Positional parameters in a subroutine definition. But not all of the Positional arguments becomes a Positional parameter!?
"Any arguments passed in show up in the array @_." This can be false!? A Positional argument can be flattened away.
Split does not behave like a subroutine
In perlfun it is stated: "Here are Perl's functions (including things that look like functions, like some keywords and named operators) arranged by category"
What is split? A keyword, named operator or ... .
It is also stated:
Any function in the list below may be used either with or without parentheses around its arguments. (The syntax descriptions omit the parentheses.) If you use parentheses, the simple but occasionally surprising rule is this: It looks like a function, therefore it is a function.
However split does not behave like a Perl subroutine. So the statement "therefore it is a function" does not mean that split behaves like a Perl subroutine.
The evaluation of the first argument to split is delayed. This is different from normal subroutines. Is this indicated by the slashes in /PATTERN/?
This script shows that an array can not be used in the arguments to split. It is also different from normal subroutines.
Is this a bug?
I had expected this