in reply to Using grep and glob to find directories containing file
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using grep and glob to find directories containing file
by Anonymous Monk on Feb 04, 2013 at 16:42 UTC | |
by Corion (Patriarch) on Feb 04, 2013 at 16:49 UTC |