Also what happens when glob is called in a function that is embedded in a loop?
Let's find out:
use strict;
use warnings;
use 5.012;
sub do_stuff {
glob "dir1/f*";
}
for my $i (1..10) {
print "$i: ";
if (my $x = do_stuff()) {
print "\t$x";
}
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
perl says the context of the glob iterator is still a loop. And it doesn't matter how deep the subs are nested:
use strict;
use warnings;
use 5.012;
sub do_stuff {
get_glob(shift);
}
sub get_glob {
glob shift;
}
for my $i (1..10) {
print "$i: ";
if (my $x = do_stuff('dir1/f*')) {
print "\t$x";
}
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
the behavior for example of glob used somewhere deep in a module funciton would vary depending on whether it was at some level called from something in a loop
I can't figure out an example of that. Edit -- okay, here is an example that shows how a function that relies on the behavior of glob inside the function can produce faulty results when the function is called in a loop:
use strict;
use warnings;
use 5.012;
sub do_stuff {
my $file_pattern = shift;
#No loop in sight...
my $d = glob $file_pattern;
if ($d) {
say $d; #...so expect 'dir1/f1'
}
#No loop in sight...
my $e = glob $file_pattern;
if ($e) {
say $e; #...so expect 'dir1/f1' again
}
}
for my $i (1..10) {
do_stuff("dir1/f*");
}
--output:--
dir1/f1
dir1/f1
dir1/f2 #uh oh
dir1/f2 #no no
dir1/f1
dir1/f1
dir1/f2 #Darn
dir1/f2 #darn darn
dir1/f1
dir1/f1
dir1/f2 #But, but...the docs...
dir1/f2 #I'm fired?? !$#!#@$!@#!!!!
dir1/f1
dir1/f1
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.
|
|