SPUG: interesting while() behavior and hosting recs

DeRykus, Charles E charles.e.derykus at boeing.com
Thu Sep 30 05:27:25 CDT 2004


An array (AV) can meaningfully be evaluated in scalar context so
I'd expect @empty to fail. However, () is a list in scalar context 
which doesn't behave in a predictable manner. 

perl -MO=Deparse -e 'my $count = 1;^Jmy @empty=(); while(@empty) {  >
my $count = 1;
my(@empty) = ();
while (@empty) {
    print "hello world\n";
    last if ++$count > 3;
}
-e syntax OK

Compare with an empty list:

perl5.6.1 -MO=Deparse -e 'my $count = 1;
     while() {     # this evaluates to true(!)
       print "hello world\n";
       last if (++$count > 3);
     }'
     my $count = 1;
     for (;;) {
       print "hello world\n";
       last if ++$count > 3;
     }
     -e syntax OK

In perl5.8.4, an equivalent variant:

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


perldfaq4 and perltrap have some warnings about lists in scalar context...
(tends to just evaluate like the comma operator, etc.). An empty list has
some very unintuitive effects apparently because it's, well,  empty, I
guess :).

Speaking of lists in scalar context, look at the effect of a non-empty
list here. Perl clearly lets you knowthat it has no idea what you're 
trying to do ;)


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


--
Charles DeRykus






 




-----Original Message-----
From: Joel Grow [mailto:joel at largest.org] 
Sent: Wednesday, September 29, 2004 10:36 PM
To: spug-list at mail.pm.org
Subject: SPUG: interesting while() behavior and hosting recs


Hey Perl gurus, I recently noticed this very strange (to me at least)
while() behavior:

 my $count = 1;
 while() {     # this evaluates to true(!)
    print "hello world\n";
    last if (++$count > 3);
 }

Prints "hello world" 3 times.  How is () evaluated to true?!?

If I put "0", "undef", "", or a scalar set to any of those in the parens, it does what I expect--it skips the while loop.  I thought "Hmm, maybe this is a strange empty list issue?", but no, the code below still skips the while loop:

 my $count = 1;

 my @empty = ();

 while(@empty) {  # skips this while loop, as I'd expect
    print "hello world\n";
    last if (++$count > 3);
 }

In desperation, I thought maybe while() was evaluating $_ by default, since you can of course do:

 while (<STDIN>) {

but printing out $_ just before the while() loop showed it is undefined.

What's going on?

Also, does anyone have a web hosting company recommendation?  I'm doing some tech work with a nonprofit and they're currently using XO.  They have a Unix account with Perl access, but the only shell access is through this thing called VDE (virtual domain environment), basically a sandbox for CGI scripts.  My main complaint is I'd have to telnet to the VDE.  And to get mysql access they'd have to spend $50/month.  I use pair.com for a personal site (and am generally happy with it), and have some experience with johncompanies.com.  johncompanies.com is cool, but for this I'd like someone else to take care of making sure the web and mail servers are running and doing software updates.  So a relatively simple unix account with Perl and rdbms (mysql or postgres preferably).

Thanks!

Joel _____________________________________________________________
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