Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Split does not behave like a subroutine

by LanX (Saint)
on Jul 18, 2020 at 07:00 UTC ( [id://11119479]=note: print w/replies, xml ) Need Help??


in reply to Split does not behave like a subroutine

> Is this a bug?

Perl doesn't have a list data type but list operators.

The scalar comma operator returns the last element.

An empty list in scalar context is undef.°

HTH

Update

> The differences between split and a normal subroutine should be clarified.

a default subroutine has no prototype, split has.

(Still, there are anormal functions without simple prototype )

Update

Sorry split is one of those built-ins without reproducible prototype, hence it's more complex:

DB<4> x prototype "CORE::push" 0 '\\@@' DB<5> x prototype "CORE::split" 0 undef

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

°) you seem to belive that naked parentheses impose list context and create a list value, but they are only grouping precedence !

... see List's terminology and Parentheses in Perl for details.

Replies are listed 'Best First'.
Re^2: Split does not behave like a subroutine
by bojinlund (Monsignor) on Jul 19, 2020 at 06:24 UTC

    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:

    • The return value context ( scalar or list ) controls how the return value is created.
    • The side effects (everything but the return value) of calling something does not depend on the return value context.

    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_arg: The list of arguments in the List value constructor [LVC0]
    • LVC_par: The resulting parameter list value
    • LVC_list_value: the list value which is the output from the constructor in list context

    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 [?].

      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"

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11119479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-25 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found