Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Seeking Perl docs about how UTF8 flag propagates

by raygun (Scribe)
on May 17, 2023 at 21:39 UTC ( [id://11152270]=note: print w/replies, xml ) Need Help??


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

There's no documentation on the choice of internal storage format, because it's an internal detail.
...except when it affects the documented behavior of functions like lc in certain situations.
Neither operators nor subs can return arrays, just scalars.
I don't follow your meaning here. In what sense is this not an array being returned:
@words = split(/ /, 'This is a sentence.');
Returning strings from a sub should have no effect on their storage format.
...yet sometimes does, per Re^7: Seeking Perl docs about how UTF8 flag propagates.

Replies are listed 'Best First'.
Re^3: Seeking Perl docs about how UTF8 flag propagates
by ikegami (Patriarch) on May 17, 2023 at 22:29 UTC

    ...except when it affects the documented behavior of functions like lc in certain situations.

    As I previously mentioned, lc is (intentionally) buggy (for backwards compatibility) when not using the unicode_strings feature. So yes, it makes sense to document it.

    In what sense is this not an array being returned:

    In every sense. Four scalars are returned by split, which are then assigned to an existing array.

    ..yet sometimes does

    No. split is a function (named operator), not a sub.

    But while I won't rule out the possibility of a change for subs, it's definitely not possible for a function. "Returning" scalars from a function definitely has no effect on their internal storage format. That post shows no evidence that returning a value had any effect on it whatsover. The returned scalars are as the function created them.

Re^3: Seeking Perl docs about how UTF8 flag propagates
by pryrt (Abbot) on May 18, 2023 at 17:43 UTC
    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
      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 );

        ... ?

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-29 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found