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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|