Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > I don't understand what glob does here

Reply
Thread Tools

I don't understand what glob does here

 
 
Darren Dunham
Guest
Posts: n/a
 
      08-06-2005
I don't understand the behavior of the following...

$ touch "file"
$ perl -e 'foreach $num (0 .. 4) { print "$num .. "; print (scalar glob("file")); print " "; print (scalar glob("file")); { print " ok\n"; } }'
0 .. file file ok
1 .. ok
2 .. file file ok
3 .. ok
4 .. file file ok

I thought that if I were to get "undef" returned on the second scalar
invocation, I would have seen one "file" in each iteration, not 2
successful followed by 2 unsuccessful.

Why does the behavior change between iterations of the foreach loop
here? I've read through glob and File::Glob and I haven't seen anything
that appears to explain this.

Thanks.

--
Darren Dunham
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-06-2005
Darren Dunham wrote:
> I don't understand the behavior of the following...
>
> $ touch "file"
> $ perl -e 'foreach $num (0 .. 4) { print "$num .. "; print (scalar glob("file")); print " "; print (scalar glob("file")); { print " ok\n"; } }'
> 0 .. file file ok
> 1 .. ok
> 2 .. file file ok
> 3 .. ok
> 4 .. file file ok
>
> I thought that if I were to get "undef" returned on the second scalar
> invocation, I would have seen one "file" in each iteration, not 2
> successful followed by 2 unsuccessful.
>
> Why does the behavior change between iterations of the foreach loop
> here? I've read through glob and File::Glob and I haven't seen anything
> that appears to explain this.


Each individual glob() call maintains its own state. The first time
through the for loop is the first call to glob("file") for each glob()
statement. The second time through the loop is the second call to each
glob().

Each glob() only returns the "next" value (undef in the case of only
one match) for successive calls to the *same* glob() statement,
regardless of whether or not a different glob() statement has been
called with the same argument.

$ perl -le 'print (scalar glob("file")); print (scalar glob("file
"));print (scalar glob("file"));print (scalar glob("file"));'
file
file
file
file
$

$ perl -le 'print (scalar glob("file")) for 0..3'
file

file

$

Hope this helps,
Paul Lalli

 
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
glob.glob output Hitesh Python 6 03-13-2007 03:45 PM
glob.glob unicode bug or feature Elbert Lev Python 5 08-02-2004 12:09 AM
Question about glob.glob <--newbie Sean Berry Python 3 05-04-2004 05:34 PM
RE: Bug in glob.glob for files w/o extentions in Windows Tim Peters Python 1 12-01-2003 09:22 AM
Bug in glob.glob for files w/o extentions in Windows Georgy Pruss Python 15 12-01-2003 04:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57