Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Why does glob() work in while loop?

Reply
Thread Tools

Why does glob() work in while loop?

 
 
J. Romano
Guest
Posts: n/a
 
      01-27-2005
Dear Perl community,

I was recently explaining to a friend new to Perl about while loop
conditions and how they implictly assign a value to $_ only if the
condition looks like:

while (<HANDLE>)

and not like:

while (function())

To demonstrate an incorrect example, I executed this following code
that contains glob() in the while condition:


#!/usr/bin/perl
use strict;
use warnings;

while (glob("*"))
{
print "$_\n";
}
__END__


I was surprised to see that the program worked as expected! (That
is, it listed every file in my directory.) I didn't expect it to do
that, because I didn't think that the while condition would populate
$_ for me.

I played around a little more, and came up with a new script:


#!/usr/bin/perl
use strict;
use warnings;

{
my $num = 0;

sub getNextNum
{
return ++$num;
}
}

while (getNextNum())
{
print "$_\n";
sleep(1);
}
__END__


Instead of calling the glob() function in the while condition, the
getNextNum() function is called, which just returns an integer (the
first time it is called, it returns 1; every other time, it returns a
number one greater than the previous call).

Now, when I run this program, those new to Perl might expect that
the return value gets put in $_ and then gets printed out. Instead, I
get:

Use of uninitialized value in concatenation (.) or string at ... line
16.

which makes sense to me, because I know that the return value of
getNextNum() is never placed in $_.

But if the return value of getNextNum() is not placed in $_, then
doesn't that mean that the return value of glob() also should not be
placed in $_? If my reasoning is correct, why does the first program
I gave populate $_?

Thanks in advance for any explanations.

-- Jean-Luc
 
Reply With Quote
 
 
 
 
Jay Tilton
Guest
Posts: n/a
 
      01-27-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (J. Romano) wrote:

: #!/usr/bin/perl
: use strict;
: use warnings;
:
: while (glob("*"))
: {
: print "$_\n";
: }
: __END__
:
:
: I was surprised to see that the program worked as expected! (That
: is, it listed every file in my directory.) I didn't expect it to do
: that, because I didn't think that the while condition would populate
: $_ for me.

[snip]

: But if the return value of getNextNum() is not placed in $_, then
: doesn't that mean that the return value of glob() also should not be
: placed in $_? If my reasoning is correct, why does the first program
: I gave populate $_?

This is explained in the documentation for the glob function. It's got a
splash of DWIM built in.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does this return fail to work while the other doesn't? The Cool Giraffe C++ 6 02-14-2007 08:50 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
int('2.1') does not work while int(float('2.1')) does Vineet Jain Python 9 04-16-2004 10:12 AM



Advertisments