in reply to Re (tilly) 9: Hash slices ?
in thread Hash slices ?

I am loath to even reply, so I'll try to keep it short.

There is no .. in the examples we have been discussing. I see a few elipses that look like Perl's ... operator but that clearly aren't meant as range nor flip-flop operators in that setting.

One of the examples has elipses while the other two don't. Are we supposed to draw some deep conclusion about the reasoning behind that? Two of the examples are short and one is long. I have an "absurd theory" that the long example has an elipsis and is missing the "same as" for the same reason: because it is long and forcing a line wrap would reduce the readability.

I did a quick search to see if I could turn up previous discussions about the fact that many details of Perl's behavior are intentionally not documented. The basic idea is that some behaviors aren't particularly important and it is good to leave things open in case later optimizations or enhancements change these "edge" behaviors (intentionally or accidentally). Unfortunately, old Usenet isn't well archived (or at least indexed and available to me) at the moment.

The behavior of an array slice in a scalar context seems a quite reasonable example of such to me. Reading the code, I get the impression that the current behavior is more an afterthought during implementation than a careful design decision. (Not to mention the fact that it isn't documented anywhere [*cackle*].)

But anyway, the interesting bit about the search was that it turned up this:

NETaa13671: array slice misbehaved in a scalar context
From: Tye McQueen
Files patched: pp.c
 A spurious else prevented the scalar-context-handling code from running.
which I got a good chuckle out of. Apparently, long ago, I actually patched Perl to fix "array slices in a scalar context" (I haven't yet been able to summon a memory of this). Anyway, I thought that was pretty funny. (:

So it is possible that array slices in a scalar context didn't even "work" before I came along.

Looking at a modern Perl (5.6.0) I see no tests for the behavior of an array slice in a scalar context (more evidence, to my mind, that this is considered an "edge" case).

But none of this matters much. You have been completely unswayed by all of my erudite elocutions while I find your "absurd" and elaborate theories about how to claim that this simple behavior is (magically, implicitly) "documented" increasingly comical. q-: (You may have to read that with a good, pompous tone of voice to get the silliness of it.)

That is to say, we aren't getting anywhere so let's just stop! I know I'm quite tired of it and I suspect that most (if there are any) of those that have read this far are getting tired of it as well.

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

Replies are listed 'Best First'.
Re (tilly) 11: Hash slices ?
by tilly (Archbishop) on Apr 16, 2001 at 03:57 UTC
    I am trying to figure out your "clearly aren't meant" statement and I am failing.

    Here are a few lines from perldata:

    but entire arrays or array slices are denoted by '@', which works much like the word "these" or "those": @days # ($days[0], $days[1],... $days[n]) @days[3,4,5] # same as @days[3..5] @days{'a','c'} # same as ($days{'a'},$days{'c'}) and entire hashes are denoted by '%': %days # (key1, val1, key2, val2 ...)
    Here are the lines of code together with how I read them.
    # An array is written like @days and can be thought # of as a list of values @days # ($days[0], $days[1],... $days[n]) # Arrays can be sliced, and a slice of a literal list # is the same as taking a slice of something that # returns a list (in this case a range operator) @days[3,4,5] # same as @days[3..5] # Hash lookups can also be sliced, and a slice of a # literal list is the same as taking a literal # list of lookups. @days{'a','c'} # same as ($days{'a'},$days{'c'}) # You write a hash like %days, and you can think of # it as a list of key/value pairs. %days # (key1, val1, key2, val2 ...)
    Now please explain why I shouldn't take the .. as being a range operator in list context. That is what it looks like was meant, and that fits perfectly...

    Also note that the "can think of" that I inserted has an implicit, "but that isn't quite correct" inserted...

      Because your copy of perldata is different from mine. Mine has no ".." in the second example. You are looking at an old version.

              - tye (but my friends call me "Tye")
        Now when did they change that? More importantly I would like to know why.

        For the record the version that I pulled from is 5.005_03, and it is merlyn's version as well as you can see if you look at what he quoted Re: Re: Re: Hash slices ?.

        Anyways I also see a brand spanking new section on slices (which is probably why it was pulled) that does not document this case (which should be fixed). As of 5.6.0 (I don't have 5.6.1, so someone should look and send in a patch) it had a typo, an attempt to swap the first and last terms of @folks was messed up when they tried to write it without a slice.

        So it used to be determinable from the documentation with 5.005_03 (which is hardly, given the problems with 5.6.0, a horribly outdated release to be still running) but is now not. In the old version the relevant snippet was what merlyn posted.