Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

In reply to Re: Using grep and glob to find directories containing file by 7stud
in thread Using grep and glob to find directories containing file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (16)
    As of 2015-07-02 10:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (33 votes), past polls