SPUG: interesting while() behavior and hosting recs

DeRykus, Charles E charles.e.derykus at boeing.com
Fri Oct 1 15:39:01 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.

Gawd, you're good if you found that somewhere in the man pages somewhere.

> 
> 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, 
> $ 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'); 

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

Alright shallow magic then :).

Hey,  if it's all so logical,  why does 'while ( () )' work like 'while (@empty)' 
  while  'while ()'  doesn't.... :)

Cheers,
--
Charles DeRykus  

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