Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: tr doesn't *really* use a list

by chipmunk (Parson)
on Dec 27, 2000 at 23:20 UTC ( #48509=note: print w/replies, xml ) Need Help??

in reply to tr doesn't *really* use a list

I would say that tr's "parameters" are more like a character class than a list. In particular, you can write a-z to mean all the letters from a to z. Calling it a character class, however, might suggest that it supports \w, \d, \s, etc.; unfortunately those are only available in the regular expression operators.

On the other hand, it is certainly inaccurate to say that a list in Perl is an anonymous array. Although this code features a list:   print "Hello ", $name, ", how are you?";
it does not feature an anonymous array.

I would say that a list in Perl is any grouping of scalars (including groupings of zero or one scalar) on the stack. An array is a list of scalars for which memory has been allocated especially (outside the stack), and an anonymous array is an array where there is no symbol name that refers to the array's memory.

Replies are listed 'Best First'.
(tye)Re2: tr doesn't *really* use a list
by tye (Sage) on Dec 28, 2000 at 00:05 UTC

    I would say that a list in Perl is any grouping of scalars (including groupings of zero or one scalar) on the stack.

    Then ($a,$b,$c) isn't a list. It might produce a "list" (using your definition), if used in a list context.

    I certainly consider ($a,$b,$c) a list. It is a list of scalar expressions which has everything to do with the syntax of the construct and nothing to do with some Perl data structure which certainly exists (in multiple forms) but that people keep wanting to say is the one true meaning of "list" when dealing with Perl.

    "List" can mean a list of scalars in memory, much like a Perl array, but it can also mean many other things. I find people repeatedly getting into trouble due to attempts to nail down the definition of a "Perl list".

    There is no one definition of a "Perl list".

    Update: and the reason this leads to trouble is that even if we restrict ourselves to the standard documentation that comes with Perl, the word "list" is used to mean many different things in that documentation. Sometimes the word "list" is used to mean different things by the same author in the same paragraph. So if you have a "nailed down" meaning for "list", some part of the Perl documentation will tell you wrong things (like that tr takes a list of scalars).

    Note that I don't think we should "fix" this by only using "list" to mean only one thing in the Perl documentation. I'd hate to see the ton of "Oops, I wrote 'list' but that wasn't a Perl List(tm) I was talking about so I'll have to call that a... uh.. 'set'... no, hmm... 'collection', uh, 'tuple'. Yes 'tuple'!"

            - tye (but my friends call me "Tye")
      Yes, ($a, $b, $c) is a list in that, syntactically, there are some scalars and they are separated by commas. But I don't think that's the interesting sense of 'list' in the context of Perl. In: $x = ($a, $b, $c)
      semantically, ($a, $b, $c) do not constitute a list because they are never on the stack together. That's why that code assigns $c to $x, whereas: $x = @a = ($a, $b, $c)
      assigns 3 to $x. It is just as easy for someone to get into trouble by not understanding what is meant by a Perl list.

      In the documentation, the word 'list' certainly appears in places where it does not mean "a grouping of scalars on the stack". I don't think it follows from that, however, that there cannot be a specific meaning for 'list' as shorthand for 'Perl list'.

        I don't think saying (to boldly paraphrase you) that "($a,$b,$c) isn't really a List" helps people to understand Perl better. I agree that there is an important concept here. I disagree strongly that you should try to call this concept just "list".

        But you did give me an idea for a "solution". We need a new term for the Perl data structure that is a list of scalar values (like "associative arrays" are now always called "hashes").

        I'm tempted to suggest using this new term in place of "list" in "list context" (to give it more power to replace the existing "list" meme), but I think the "list" in "list context" is intentionally vague since it refers to the "left-hand side" rather than the result returned from the "right-hand side".

        The best candidate I've come up with is "tuple".

        Oh, well, it would never fly. It is much easier to argue that "length(@array)" is stupid and look how much support has been thrown behind the current definition when that has been brought up.

        I'll just continue to use "list of scalar values", "operator that would return a list of scalar values if used in a list context", and "argument list" when I mean of the three most common forms of "Perl List". :-} (I don't yet have a good label for what goes between the parens in a foreach statement -- it is naively a "list of scalar values" but is functionally much more like an "argument list" but calling it the latter would confuse people.)

        Update: I'm toying with "list of scalar [L]values" for "argument list" and foreach. (: (and I have mentioned recently that I hate fonts that make "l"s look like "1"s?)

                - tye (but my friends call me "Tye")

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://48509]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-04-21 16:02 GMT
Find Nodes?
    Voting Booth?