Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^4: Using grep and glob to find directories containing file

by Anonymous Monk
on Feb 03, 2013 at 17:21 UTC ( #1016840=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Using grep and glob to find directories containing file
in thread Using grep and glob to find directories containing file

I still think something is wrong...

If I do something like:

mkdir dir1 dir2 touch dir1/f{1,2}
and then run:
foreach (1,2,3,4) { print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*"); }
I get 'dir1' printed 3 times (NOT 4). But if I unwind the loop:
print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*"); print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*"); print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*"); print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*");
Then I get 'dir1' printed 4 times!!!

In other words, I can understand why 'glob' is stateful when it is the loop iterator. But it makes no sense (to me) (and seems wrong) for it to be stateful when it is not the iterator. Because then unwinding a loop give a different answer.


Comment on Re^4: Using grep and glob to find directories containing file
Select or Download Code
Re^5: Using grep and glob to find directories containing file
by 7stud (Deacon) on Feb 03, 2013 at 18:03 UTC

    foreach (1,2,3,4) { print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*"); }
    I get 'dir1' printed 3 times

    I would think your code would print 'dir1' twice. In your second code example, there is no advancing iterator because you can double the number of print statements and you will get double the output.

    Your first example doesn't tell the whole story. Look at this output:

    use strict; use warnings; use 5.012; for my $i (1..10) { print "$i: "; if (my $x = glob "dir1/f*") { print "\t$x"; } if (my $y = glob "dir2/f*") { print "\t$y"; } print "\n"; } --output:-- 1: dir1/f1 2: dir1/f2 3: 4: dir1/f1 5: dir1/f2 6: 7: dir1/f1 8: dir1/f2 9: 10: dir1/f1

    So it appears that inside a loop, once the iterator is exhausted, the next call to glob() returns undef, then the next call to glob() creates a new iterator(or resets the old iterator) and returns the first matching filename again.

    You can run a similar test on your second example:

    use strict; use warnings; use 5.012; if (my $x = glob "dir1/f*") { say $x; } if (my $x = glob "dir1/f*") { say $x; } if (my $x = glob "dir1/f*") { say $x; } if (my $x = glob "dir1/f*") { say $x; } if (my $x = glob"dir1/f*") { say $x; } --output:-- dir1/f1 dir1/f1 dir1/f1 dir1/f1 dir1/f1

    No loop, no call to the iterator's next() method. So as is often the case, perl examines the context of the statement to determine its return value.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016840]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2014-09-21 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls