2/9/2024 0 Comments Grep directory![]() I don't understand why the elements of glob("$_/f*") returned for the first directory entry seemingly spill over to subsequent directories in the (implicit) grep iteration. The glob finds no elements in the other 2 directories, so it should be undefined which would evaluate as false. Since the first expression glob("$_/f*") is only true for 'dir1', it should only return that element of the directory list (even though glob returns 3 files in that directory). I thought grep will *only* return the elements of the array for which the first expression is true. In the absence of evidence, opinion is indistinguishable from prejudice. Grep will return the input (eg."dir1", "dir2", "dir3"), if the glob returns true. Re: Using grep and glob to find directories containing file 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. Most likely, part of the documentation of keys can be adapted to be added to the glob documentation. I guess that the best solution is to not call glob in scalar context at all. ![]() I think the problem is most similar to the problem of keys (not) resetting the iterator over a hash. If so how does one properly report such an issue? ![]() But currently, the documentation at best alludes rather obscurely to the behavior that can lead to an issue in such a context.ĭo people agree this is a valid issue that needs addressing either in 'fixing' glob or at least in documenting and warning about the behavior? This would be bad enough if the behavior were fully or even adequately documented. Then it will lay there waiting until one day someone calls the module from a loop and gets wrong results. 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. Thanks 7stud for all your patience and persistence in helping me figure out this strange/unexpected behavior. Re^2: Using grep and glob to find directories containing file The stateful carry over part seems to be weird if not buggy. Arrays in scalar context evaluate to the number of elements, so only empty arrays are false in boolean context. 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. Re^3: Using grep and glob to find directories containing file 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'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 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |