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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.