Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Using grep and glob to find directories containing file

by 7stud (Deacon)
on Feb 03, 2013 at 23:28 UTC ( [id://1016874]=note: print w/replies, xml ) Need Help??


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
    Thanks 7stud for all your patience and persistence in helping me figure out this strange/unexpected behavior.

    It seems to me that this is a hidden and potentially significant time-bomb type issue since glob is a core function and it's not inconceivable that people will bury it somewhere in a module where it is used in static context. Then it will lay there waiting until one day someone calls the module from a loop and gets wrong results.

    This would be bad enough if the behavior were fully or even adequately documented. But currently, the documentation at best alludes rather obscurely to the behavior that can lead to an issue in such a context.

    Do people agree this is a valid issue that needs addressing either in 'fixing' glob or at least in documenting and warning about the behavior?

    If so how does one properly report such an issue?

      I think the problem is most similar to the problem of keys (not) resetting the iterator over a hash. I guess that the best solution is to not call glob in scalar context at all.

      Most likely, part of the documentation of keys can be adapted to be added to the glob documentation. I would open a bug report using the perlbug utility, best together with a proposed documentation patch that cautions against using glob in scalar context.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found