in reply to Re: Split does not behave like a subroutine
in thread Split does not behave like a subroutine

Thanks LanX for the answer! I have got a lot to think over.

To understand this: > The scalar comma operator returns the last element. I must understand in which list it is the last element.

I have to understand what (LIST) means in the documentation.

I believe you for Perl can say:

From List value constructors

(In the text it is very difficult to understand which parts apply to use in list context, scalar context or both.)

LVC1: List values are denoted by separating individual values by commas (and enclosing the list in parentheses where precedence requires it): (LIST)

LVC2: In a context not requiring a list value, the value of what appears to be a list literal is simply the value of the final element

LVC3: LISTs do automatic interpolation of sublists. That is, when a LIST is evaluated, each element of the list is evaluated in list context, and the resulting list value is interpolated into LIST just as if each individual element were a member of LIST.

LVC4: The null list is represented by (). Interpolating it in a list has no effect.

From Comma Operator

CO1: Binary "," is the comma operator.

CO2: In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value.

CO3: In list context, it's just the list argument separator, and inserts both its arguments into the list. These arguments are also evaluated from left to right.

My own definitions

Several (possible) lists can bee involved:

LVC_scalar_value: the scalar value which is the output from the constructor in scalar context

My first try to: The execution of syntactic construct (LIST)

The elements in LVC_arg are evaluated from left to right according to [CO2] or [CO3]. [CO2] and [CO3] gives the same execution order and the same side effects. The only difference is how the return value is created.

The execution of a element in LVC_arg is recursive according to [LVC3]. During this is the LVC_list_value created. Note that this execution of the elements are done in list context.

When the (LIST) is called in scalar context the LVC_list_value is not used. Instead is the LVC_scalar_value created and returned. The LVC_list_value is the value of the last element, in the last recursion. The execution of the last element is done in scalar context. An execution of the empty list, (), in list context is a "no operation" [LVC4]. The execution of an empty list in scalar contexts results in undef [?].

  • Comment on Re^2: Split does not behave like a subroutine

Replies are listed 'Best First'.
Examples for "LIST", "list context", "list value", "list assigment", "list operator"
by LanX (Sage) on Jul 19, 2020 at 13:55 UTC
    Sorry that's too complicated for me, TL;DR

    > I have to understand what (LIST) means in the documentation.

    According to the docs cited in

    List's terminology and Parentheses in Perl

    <updated>

    Uppercase "LIST" in perldocs is a argument placeholder for any code ...

    • compiled in "list context"
    • delivering a "list value" at run time

    For instance from the docs for print ...

    print LIST; means that ...

    • print 1,2,3;

      the two commas are compiled to create a "list value" 1,2,3 for print

    • print @a;

      the array @a will be compiled to pass it's content as "list value" ( i.e.not his length from scalar context)

    • print func();

      the sub &func will be called in "list context" at run time and return a "list value" from it's last statement. ( compare wantarray )

    </updated>

    > To understand this:

    > > The scalar comma operator returns the last element.

    > I must understand in which list it is the last element.

    Comma separated lists (sic)

    Please compare (updated)

    # --- list assignments @a = (1,2,3); # list comma @a = (1..3); # list range ($a) = @a; # deconstruction => $a==1 # --- scalar assignments $a = (1,2,3); # scalar comma $a = (1..3); # flip flop operator ... Oo oops! $a = @a; # scalar @a == length

    The parens on RHS only do grouping here they do not create a list. Hence the context is propagated to the operators , and ..

    @a = and (...)= are "list assignment" imposing "list context".

    The range 1..3 returns the list value 1,2,3 in list context only.

    You have to think of the whole statement as an op-tree...

    • propagating context down
    • passing "values" up

    With ...

    • operators changed by upper context
    • operators (sometimes) creating down context

    If that's not clear enough please show a short code exemplifying your problem.

    HTH

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    update

    you may want to play around yourself with B::Deparse and B::Concise to experiment around with the snippets shown

    perl -MO=Deparse,-p -e "CODE"

    and

    perl -MO=Concise -e "CODE"

Re^3: Split does not behave like a subroutine (inconsistent perldocs)
by LanX (Sage) on Jul 19, 2020 at 18:44 UTC
    The docs were written by different persons, and are hence heterogeneous and not consistent.

    (kind of "there is more than one way to word it" ... well )

    Even perlglossary which originates from the Camel-Book seems to have been patched afterwards.

    For a trained mathematician used to an axiomatic approach it's a painful experience ... >(

    So I'd strongly recommend to

    • use perlglossary for definitions
    • use tests to proof a thesis (runtime)
    • use B::Deparse and B::Concise for visualization (compilation)

    In an ideal world any other perldoc should be corrected.

    I never acquired the Camel book I know it's vast but should be canonic.

    It's a long time since I had a look into chromatics "Modern Perl", but it left very consistent impression. So maybe a good source, not sure if he clarifies "lists" there.

    update

    Modern::Perl#Lists

    confusing too.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Thanks LanX for the effort and answers! I am very grateful.

      To understand something you need to create a conceptual model of it. I have realized that my model of Perl and the perl interpreter was not adequate.

      By following your advice to deparse the generated code, I have start to understand what is going on. I have also read some introductions to the perl interpreter.

      To know the use of the

      Value Stack

      This stack stores the values that regular perl code is operating on, usually intermediate values of expressions within a statement. The stack itself is formed of an array of SV pointers.

      for the transfer of parameters to and return values from subroutines, helps me to build my model.

        Yes, that's pretty much also my mental model, I'm glad I helped you.

        And rest assured many people have (unfortunately) a different one. :/

        I wished it was better communicated.

        Part of the problem is that Perl is advertising itself as kind of a natural language with a lot of DWIM and idiomatic constructs, which shows in the style of the docs.

        But at the end you need to communicate the technical basics in a clear, concise and orthogonal way to avoid confusion and frustration thru perceived ambiguities.

        Much potential is wasted here...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery