SPUG: interesting while() behavior and hosting recs

DeRykus, Charles E charles.e.derykus at boeing.com
Fri Oct 1 13:34:45 CDT 2004

On Fri, Oct 01, 2004 at 08:25:02AM -0700, DeRykus, Charles E wrote:
> But, as we've seen Joel's post, the effects of a "list in scalar 
> context" in a different may altogether different and unexpected... ie, 
> while () ... gets parsed as for (;;). Charles DeRykus

That sentence parses like a "mind in void context"...

> Specifically, what qualifies

>	while () { statement; }

> as having /anything/ to do with the conceptually understandable (albeit technically non-> > existent) "list in scalar context"?

> Or, for that matter, the parens in while's close relative:

>	if () { statement; }

> AFAIK, those parens after the keywords are *required elements of
> syntax* that have nothing to do with the kind of "list formation" effect that people 
> associate with expressions like:
>	@stuff=(1,2,3);
> and @stuff=();	# empty list

> It seems to me that earlier posts are equating the parens in "while ()..." to those in my > empty list example above, which conflicts with my interpretation that they are merely > syntactic elements after "while".

You're right. I was was over-generalizing... those parens are syntactic
requirements and shouldn't have been tossed into the "list in scalar 
context" discussion.

Maybe my "list in scalar context" just to provide a semantic handle for
for parens that don't parse or work the way you think they do is a bad
idea. But as you can see, parens are a tricky issue. John cites a whole
thread that I presume discusses some of these subtleties.

For instance, if the original expression were changed to:

  while (())   {...   # instead of while () 

There would be no problem and would work exactly the same as
  @empty = ();
  while ( @empty) {...

Yet, the inner paren's might be mistaken for a list. I'm
not totally sure what's happening.

And to repeat another subtlety I mentioned earlier, look what Perl does here:

$ perl -MO=Deparse -e '$c=1;while ( ("a","b","c") ) {
                 print "hello,world";last if ++$c>3;}' 

    $c = 1; 
    while ('???', '???', 'c') {
       print 'hello,world';
       last if ++$c > 3;
-e syntax OK

The ("a","b","c") might easily convince someone they had a  "list in scalar context"
even though it's not.  But Perl treats this altogether differently than
$scalar = ("a","b","c") where the list would resolve with a comma operator.

Deep magic.

Charles DeRykus


This is one of the really difficult areas. I think that's why the doc's gloss 
over much of the fine print.
> I'll readily admit that I, like many JAPHs, have some of my own ways of thinking 
> about Perl that make it easier for me to use, and to teach, and that usually serve me 
> quite well -- despite their technically being over-simplifications of Perl's (twisted) 
> reality. 8-}

> As a case in point, I offer the widespread notion that parens function as "list 
> constructors" in expressions like "@array=(1,2)" and "print (1,2)", which is 
> tremendously useful, despite being less than a perfectly accurate representation 
> of how those expressions are actually parsed and evaluated, IIRC. (What's the real
> story? Is it that the comma operator in list context returns all the expressions,
> so the parens are just there for reasons of precedence in the case of an assignment?)

> Surely, even in Perl, there are some elements of basic syntax that can be trusted 
> to have concrete meanings -- like while's parens; aren't there? 8-}

More information about the spug-list mailing list