Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
more useful options
 
PerlMonks  

Re: Using grep and glob to find directories containing file

by moritz (Cardinal)
on Feb 03, 2013 at 15:14 UTC ( #1016829=note: print w/ replies, xml ) Need Help??


in reply to Using grep and glob to find directories containing file

Weird, your code works for me:

$ mkdir dir1 dir2 dir3 $ touch dir2/foo $ perl -wE 'use Data::Dumper;print Dumper grep(glob("$_/f*"),("dir1", +"dir2", "dir3"))' $VAR1 = 'dir2';

Which is exactly what you seem to want.

Maybe your problem is actually elsewhere?

Update: I found a problem. If there's more than one match, the code skips every second dir. The reason is that glob in scalar context is stateful, and needs on extra cycle to reset. A possible fix is to evaluate the glob in list context, and check if the list has a least one element.


Comment on Re: Using grep and glob to find directories containing file
Download Code
Re^2: Using grep and glob to find directories containing file
by Anonymous Monk on Feb 03, 2013 at 16:13 UTC
    Thanks. Putting it in array context -- i.e. @{glob("$_/f*")} - did the trick. I changed the code to:
    perl -wE 'use Data::Dumper;print Dumper grep(@{[glob("$_/f*")]},("dir1 +", "dir2", "dir3"))'
    The code seems to work with that change but I just want to check that there is no need to explicitly check "if the list has at least one element" since presumably evaluating the list (by grep) will determine if it is empty or not.

    I'm still not really sure why scalar context doesn't work (indeed, I would have thought scalar context would be better than list context). The stateful carry over part seems to be weird if not buggy. But as long as it works for me by forcing an array context, then it's all good even if it was far from obvious at first glance.
      I just want to check that there is no need to explicitly check "if the list has at least one element" since presumably evaluating the list (by grep) will determine if it is empty or not.

      Right, no need for an explicit check. Arrays in scalar context evaluate to the number of elements, so only empty arrays are false in boolean context.

      The stateful carry over part seems to be weird if not buggy.

      Well,

      glob</b> needs to carry state for this useful idiom to work: <code> while (my $file = glob '*.txt') { # do something with $file } </code> <p>Which is more friendly to memory than using a list and iterating th +at.</p> <p>But one could argue that <c>glob
      should discard its internal state when called with a different argument.

        Thanks for the help and explanation!!!

        This behavior is certainly not obvious and it is not (clearly) documented either under 'perldoc -f glob' or at perldoc.perl.org. The line saying "In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted" does not make it clear (at least to me) that such a state persists across calls to glob with a new argument! In fact, I can imagine all types of unintended errors when glob has been called in a scalar context in one place and then many lines later is called again in a different context and the programmer would have no idea that the state persists.

        But then again I am not a Perl monk so these things are never obvious to me :)
        I still think something is wrong...

        If I do something like:
        mkdir dir1 dir2 touch dir1/f{1,2}
        and then run:
        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 (NOT 4). But if I unwind the loop:
        print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*"); print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*"); print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*"); print "dir1\n" if glob("dir1/f*"); print "dir2\n" if glob("dir2/f*");
        Then I get 'dir1' printed 4 times!!!

        In other words, I can understand why 'glob' is stateful when it is the loop iterator. But it makes no sense (to me) (and seems wrong) for it to be stateful when it is not the iterator. Because then unwinding a loop give a different answer.
      > grep(@{[glob("$_/f*")]}

      there are easier ways to count in list context,

      grep { () = <$_/*> } <dir{1,2,3}>

      (kind of a "half goatse" ;)

      Cheers Rolf

        Despite the admonition not to use <> for globbing?

        So...list context on the left, which then creates an anonymous array, which array, when evaluated in boolean/scalar context, produces the length of the array?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-04-17 22:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (458 votes), past polls