Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: On Scalar Context ("list")

by tye (Sage)
on Apr 23, 2004 at 18:14 UTC ( #347699=note: print w/replies, xml ) Need Help??

in reply to On Scalar Context

Thanks, merlyn++.

Note that in the following cases, "last element" doesn't mean the same thing each time.

(10, 20, 30)last element
@foo[3..5]last element
(10, 20, 30)[2, 1]last element

I'd actually change one entry:

(10, 20, 30)last expression

Because it isn't the "last element" of the "list of scalar values that this operation would produce if called in a list context" but is instead the "last element" in the "list of Perl expressions that are separated by commas". Likewise, I might change the other two to "last scalar value" to be clearer.

This is another perfect example of why "list" doesn't just mean one thing when talking about Perl. Each of those cases return the "last element" of some "list", it is just that "list" means two different things in those two cases.

I think this is a big part of why people get confused and people argue when talking about "lists" in Perl. Since "list context", "scalar context", and "scalar" all have precise definitions in Perl, it is natural to think that "list" has a precise definition as well.

#!/usr/bin/perl -wl use strict; my @idx= ( 2, 99, 5 ); my @a2z= ( 'a'..'z' ); my @arr= ( 10..16 ); print '(@arr,@a2z): ', scalar( (@arr,@a2z) ); print '(@arr,@a2z)[@idx]: ', scalar( (@arr,@a2z)[@idx] ); print '@arr[@idx]: ', scalar( @arr[@idx] );


Useless use of private array in void context at line 9. (@arr,@a2z): 26 (@arr,@a2z)[@idx]: 15 @arr[@idx]: 15

So only the first item is lazy (passing the scalar context inside such that a "list of scalar values on the stack" doesn't get generated just to be mostly thrown away).

I still believe that the other two "should" have been lazy (pass their scalar context into @idx) but instead the developers of Perl were lazy and just wrote code to generate the list of scalar values and simply pick the last one at the last minute. But since slices in scalar context are more "edge" or "corner" cases, being lazy with the design was probably the right choice. (:

So $s = (10,20,30) does contain "a list in scalar context". Either one has to admit that or one has to not say that $s gets set the the "last element of the list". :)

- tye        

Replies are listed 'Best First'.
Re: Re: On Scalar Context ("list")
by Errto (Vicar) on Apr 24, 2004 at 02:04 UTC

    Although it gets a bit hairy, one can explain that behavior by saying that my $a = (10,20,30); is merely an application of the comma operator in scalar context, whereas the rhs of my $a = (10,20,30)[0..2] is really a list, and so the behavior of taking the last value has to be explicitly defined. The distinction is important because I believe that  my $a = (1,2,foo) would call foo in scalar context, but my $a = (1,2,foo)[0..2] would call it in list context.

    The preceding paragraph will be moot someday because according to Apocalypse 3, there will be no scalar comma operator in Perl 6 - it will always be a list, and a list in scalar context will return an array reference

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://347699]
[ambrus]: Corion: well Prima::Object says something like that the cleanup method will send an onDestory message and that you can't get more messages after cleanup, or something.
[Corion]: ambrus: Yeah - I don't think the deep source dive will be necessary if things are implemented as simple as they could be :)) And hopefully I won't need (more) timely object destruction. I can update the screen at 60Hz and hopefully even do HTTP ...
[Corion]: ... transfers in the background. Now that I think about it, this maybe even means that I can run the OpenGL filters on Youtube input :)
[ambrus]: Corion: I mentioned that the unix event loop of Prima always wakes up at least once every 0.2 seconds. Have you found out whether the win32 event loop of Prima does that too?
[Corion]: ambrus: Hmm - I would assume that the onDestroy message is sent from the destructor and doesn't go through the messageloop, but maybe it is sent when a window gets destroyed but all components are still alive...
[ambrus]: Corion: partly deep source dive, partly just conservative coding even if it adds an overhead.
[Corion]: ambrus: Hmm - no, I haven't looked at wakeup intervals ... I wonder why it should want to wakeup periodically because it gets a lot of messages from the Windows message loop (on Windows obviously)
[ambrus]: (Alternately a deep source dive and then rewrite that event loop to make it better, and then as a bonus you get an idle method.)
[ambrus]: The 0.2 seconds wakeup is likely a workaround for some bug, but I can't guess what bug that is.
[ambrus]: It's been there since Prima 1.00 iirc

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2016-12-09 10:31 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.