http://qs321.pair.com?node_id=11119478

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.

Perl subroutine

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.

Comma Operator:

"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."

Conclusions:

FLAT LIST

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."

Conclusions:

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.

use strict; use warnings; use 5.010; use Data::Dump qw(dump dd ddx); sub p2 { 'P2' } sub nop1 { } sub nop2 { return } sub test { say dump @_ } test( 'P1', 'P2', 'P3' ); test( 'P1', p2, 'P3' ); test( 'P1', nop1, 'P3' ); test( 'P1', nop2, 'P3' ); __DATA__ Output: ("P1", "P2", "P3") ("P1", "P2", "P3") ("P1", "P3") ("P1", "P3")

Conclusions:

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.

use strict; use warnings; use 5.010; my $pat = ':'; my $str = 'O:K'; my $pat_ref = \$pat; my $str_ref = \$str; my @par = ( $pat, $str ); my $rv = split $pat, $str; # OK $rv = split $pat, $$str_ref; # OK $rv = split $$pat_ref, $$str_ref; # OK $rv = split( $par[0], $par[1] ); # OK # W1: Use of uninitialized value $_ in split $rv = split @par; # NOK W1 $rv = split(@par); # NOK W1 $rv = split( @par[ 0, 1 ] ); # NOK W1 $rv = split( ( @par[ 0, 1 ] ) ); # NOK W1 $rv = split( map { $_ } @par ); # NOK W1 $rv = split( ( map { $_ } @par ) ); # NOK W1

Conclusions:

Is this a bug?

#!/usr/bin/perl -w use strict; use warnings; use 5.010; use Data::Dump qw(dump dd ddx); ddx (1,'a', ()); ddx scalar (1,'a'); ddx scalar (1,'a', ()); my $var = (1,'a', ()); ddx $var; __DATA__ output: Useless use of a constant ("a") in void context at pm_empty_list.pl li +ne 11. Useless use of a constant ("a") in void context at pm_empty_list.pl li +ne 12. # pm_empty_list.pl:9: (1, "a") # pm_empty_list.pl:10: "a" # pm_empty_list.pl:11: undef # pm_empty_list.pl:13: undef

I had expected this

# pm_empty_list.pl:11: undef # pm_empty_list.pl:13: undef
to be
# pm_empty_list.pl:11: "a" # pm_empty_list.pl:13: "a"