SPUG: interesting while() behavior and hosting recs

Tim Maher tim at consultix-inc.com
Fri Oct 1 15:22:32 CDT 2004


On Fri, Oct 01, 2004 at 11:34:45AM -0700, DeRykus, Charles E wrote:
> 
> 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') {

As indicated on a man-page somewhere,
those ??? marks indicate that the actual values provided at 
those locations are irrelevant, because "c" will be the result of
this application of the comma operator.

>        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.

How does it treat it differently?  I think it's the same, as
indicated by no iterations of this variation on your example:

$ perl -MO=Deparse -e '$c=1;while ( ("a","b",0) ) {

And this deparsing of the assignment to a scalar:
	perl -MO=Deparse -e '$scalar = ("a","b","c") '
	-e syntax OK
	$scalar = ('???', '???', 'c');

Look familiar? 8-}

> Deep magic.
> Charles DeRykus
> 

I'm used to "deep magic" in Perl, but I don't detect its presence here.
Did I miss your point, or are we miscommunicating?

-- 
*--------------------------------------------------------------------------*
| 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   |
*--------------------------------------------------------------------------*


More information about the spug-list mailing list