SPUG: interesting while() behavior and hosting recs

Joel Grow joel at largest.org
Fri Oct 1 14:04:12 CDT 2004

On Fri, 1 Oct 2004, Tim Maher wrote:

> 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
> I've been skimming this thread, but having trouble with a fundamental
> element of the discussion.
> Specifically, what qualifies
> 	while () { statement; }
> as having /anything/ to do with the conceptually understandable (albeit
> technically non-existent) "list in scalar context"?

Good point, it has nothing to do with "list in a scalar context".
while() of course just checks the conditional in parens for truth, and the
parens are syntax, not a list.  The reason I brought empty list into the
discussion is that I was burned once a year or two ago with an empty list
issue, where it didn't behave as I expected.  I think it was with a regex
match, but unfortunately I can't remember exactly what the situation was.
:-)  I wanted to point it out just as a debugging thing I tried.

John pointed out:

 That is working for the same reason that

 for ( ;; ) { ... }

 works in Perl as well as in C where Perl inherited it from.

I guess that's a good enough explanation, but it still seems to me like

 while () {

should intuitively evaluate to false.  I'll get over it.  Or at least
memorize it.

Btw, if you really want to use while() without the parens, you could do:

 do {
    print "hello world\n";
 } while $x++ < 3;

# not to my tastes, but tmtowtdi...


> 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".
> If they're /not/ syntactic, why can't we write code like this?
> 	while $foo { statement; }
> 	while @foo { statement; }
> or
> 	if 1 { statement; }
> (And that code doesn't compile; I checked!)
> 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-}
> *--------------------------------------------------------------------------*
> | Tim Maher, CEO     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
> | tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
> *+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
> | Watch for my upcoming book: "Minimal Perl for Shell Users & Programmers" |
> | Classes! 10/4: UNIX Fundamentals  11/30: Perl  12/6: Shell & Utilities   |
> *--------------------------------------------------------------------------*
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
> POST TO: spug-list at mail.pm.org  http://spugwiki.perlocity.org
> ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, Location Unknown
> WEB PAGE: http://www.seattleperl.org

More information about the spug-list mailing list