Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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.

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:

  • In the documentation often list is used for both list value and list value constructor. This make it difficult to understand the documentation.
  • Sometimes there are contradictions. What applies to a list (list value), perhaps does not apply to a list (list value constructor)

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:

  • A list value is always flat.
  • The arguments to a list value constructor can be none flat.
  • The list concept and for all flattening should be described more clearly in the Perl documentation.

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:

  • The risk to lose a Positional argument should be explicit clarified
  • In conservative programming, there should be no subroutine calls in the argument list!?

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:

  • Split look like a subroutine but does not behave like one.
  • The differences between split and a normal subroutine should be clarified.
  • The meaning of the slashes in /PATTERN/ should be explained.

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"


In reply to Split does not behave like a subroutine by bojinlund

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found