in reply to Re^2: Seeking Perl docs about how UTF8 flag propagates
in thread Seeking Perl docs about how UTF8 flag propagates

I don't follow your meaning here. In what sense is this not an array being returned:

raygun, in case you haven't run across this idea yet, Perl has a distinction between a LIST and an ARRAY: split returns a LIST, not an ARRAY.

Per List value constructors, "List values are denoted by separating individual values by commas (and enclosing the list in parentheses where precedence requires it)", so a LIST is just the comma-separated sequence (which, as explained lower down in that same section, will interpolate any lists, arrays, or hashes that are included in the list).

Per perldata's DESCRIPTION, an ARRAY is a datatype that contains "ordered lists of scalars indexed by number". The term can also be used to refer to any variable or anonymous data that has the ARRAY datatype.

So in @words = split(/ /, 'This is a sentence.'); , the @words variable is an ARRAY variable, and it is being initialized by a LIST of scalars returned by the built-in function split.


edit: created separated paragraphs for clarity

Replies are listed 'Best First'.
Re^4: Seeking Perl docs about how UTF8 flag propagates
by raygun (Scribe) on May 19, 2023 at 04:51 UTC
    Perl has a distinction between a LIST and an ARRAY
    Thanks; I've seen both terms, and assumed them to be basically interchangeable—which in a lot of contexts I reckon they are. But I take your point that if split returned an array, you couldn't do something like ($word1, $word2, $word3, $word4) = split(/ /, 'This is a sentence.'). So thanks for the clarification.
      Hello raygun,

      in effect the difference is subtle and can fade into equivalence, but see it from this point of view: a list is not a Perl data type. Perl has three main variable types: scalars, arrays, and hashes. (perlintro).

      In Learning Perl - third edition is stated this way:

      > A list is an ordered collection of scalars. An array is a variable that contains a list. In Perl, the two terms are often used as if they're interchangeable. But, to be accurate, the list is the data, and the array is the variable. You can have a list value that isn't in an array, but every array variable holds a list

      Sometimes, just to make things more foggy, you can do operations on list in the same way as you do with array:

       print +( qw(J A P H) )[2] prints P

      but you cant shift a list and the doc goes: shift ARRAY but for example print accepts a list: print LIST

      Another key concept is LIST in respect of context and here it overlaps with array: LIST or SCALAR context: in list context you can assign it to an array, but again, the array is the perl variable and the list is its value.

      See also: What is the difference between a list and an array? and Scalars, Lists, and Arrays

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      if split returned an array, you couldn't do something like ($word1, $word2, $word3, $word4) = split(/ /, 'This is a sentence.')

      What am I missing in ...

      @collect = split( / /, 'This is a sentence.' ); ( $word1, $word2, $word3, $word4 ) = @collect; print join( q[ ; ], ( $word1, $word2, $word3, $word4 );

      ... ?

        What am I missing

        The $word* variables aren't getting assigned "an array", they are getting assigned the values from the array.

        @collect = split( / /, 'This is a sentence.' );
        1. The array on the LHS means that the assigment is in list context.
        2. Split returns a list of scalars.
        3. In list context, the elements of what's on the LHS get assigned to the values from the list on the RHS, so the @collect array is populated from the values returned by split
        ( $word1, $word2, $word3, $word4 ) = @collect;
        1. The (...) on the LHS mean that the assignment is in list context
        2. Per List value constructors, the @collect array gets evaluated in list context, which results in the list (on the RHS) consisting of the elements from the @collect array
        3. The assignment then happens in list context, so the leftmost item of the list on the LHS ($word1) gets assigned the value of the leftmost item from the list on the RHS, and so on
        print join( q[ ; ], ( $word1, $word2, $word3, $word4 ); => syntax error at parv.pl line 3, near ");"

        but with a second ) on that line to allow it to compile:
        1. the words are given list context by the parentheses (unnecessarily, because join already gives list context)
        2. join concatenates the elements of that list, using space-semicolon-space as the separator, into a string
        3. That string is then printed.

        (LHS = "Left-hand side", RHS = "Right-hand side": in these cases, relative to the assignment operator =)

Re^4: Seeking Perl docs about how UTF8 flag propagates
by ikegami (Patriarch) on May 18, 2023 at 18:51 UTC

    split returns a LIST, not an ARRAY.

    It does not return a "LIST". There's no such data structure. As I said, it returns scalars, which is to say it adds scalars to the stack.

    Colloquially, we do say it returns a list (of scalars). By that, we simply mean it returns (a number of) scalars.

    Scalars is the only thing being returned. No list. No array.

    (As for "LIST" spelled like that, the docs use this to refer to an expression evaluated in list context, such as the arguments to print. split most definitely does not return an expression.)

      It does not return a "LIST". ... No list

      Colloquially or not, the documentation I referenced specifically states that split returns a list, and I quote: "Splits the string EXPR into a list of strings and returns the list in list context". As such, "list" is canonical terminology for what split returns in list context, until such time as it is removed from the docs. I will continue to maintain that split does return a list, your "No list." notwithstanding.

      If you are instead quibbling with my use of ALL CAPS to try to show it as a term, would you prefer I had said "list" (with "air-quote"-style quotes) instead? (In evidence of the fact that I was using ALL CAPS to be a term that I am trying to define, I also did the ALL CAPS for ARRAY, and that's not how the documentation refers to the data type, either.)

      There's no such data structure.

      In my paragraph on LIST or "list" or however you want me to type "the concept of list in Perl documentation", I never once called it a "data structure" or "data type". Contrariwise, I very specifically called an ARRAY or "array" a "data type" (well, I typed "datatype", sorry) because that's what the docs call it.

      I was trying to help the O.P. differentiate concepts: the concept of the list construct (and with my link, obliquely to list context) compared to the concept of the array data type.

      As for "LIST" spelled like that, the docs use this to refer to an expression evaluated in list context

      Can you point me to a document that makes this distinction? I was trying to see if they had a formal definition of LIST in all caps anywhere, but couldn't find it; the List value constructors was the only section that I could readily find that tries to define a "list" of any sort.

        I never once called it a "data structure" or "data type".

        But you did say it's returned instead of an array. You didn't use the words "data structure" or "data type", but you repeatedly presented it as something comparable to an array.

        And then you started to mix two different meanings of list. (It does not return a "list value", which refers to Perl code.) "List" is a dangerous word to use.

        Can you point me to a document that makes this distinction?

        Here's where the documentation uses LIST

        chmod LIST chomp( LIST ) chop( LIST ) chown LIST die LIST exec LIST exec PROGRAM LIST formline PICTURE,LIST grep BLOCK LIST grep EXPR,LIST import LIST join EXPR,LIST kill SIGNAL, LIST map BLOCK LIST map EXPR,LIST no MODULE VERSION LIST no MODULE LIST open FILEHANDLE,MODE,EXPR,LIST pack TEMPLATE,LIST print FILEHANDLE LIST print LIST printf FILEHANDLE FORMAT, LIST printf FORMAT, LIST push ARRAY,LIST reverse LIST say FILEHANDLE LIST say LIST sort SUBNAME LIST sort BLOCK LIST sort LIST splice ARRAY,OFFSET,LENGTH,LIST sprintf FORMAT, LIST syscall NUMBER, LIST system LIST system PROGRAM LIST tie VARIABLE,CLASSNAME,LIST unlink LIST unshift ARRAY,LIST use Module VERSION LIST use Module LIST utime LIST warn LIST

        The syntax used to describe the syntax of Perl operators is not documented, but it is consistent. LIST always represents an expression evaluated in list context. (Contrast that with my, our and state which use VARLIST.)