Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

More on directory recursion

by greenhorn (Sexton)
on Jun 12, 2000 at 22:13 UTC ( #17743=perlquestion: print w/replies, xml ) Need Help??

greenhorn has asked for the wisdom of the Perl Monks concerning the following question:

Egad, but I love this place. I could stay here<KBD> while(1); ...</KBD>:)
The recent thread on directory recursion prompts a question:
With scant experience with Perl and no grasp of how a recursive
function works, I was stumped by this same problem. An unusually
patient C programmer at work explained recursion to me enough times that
I finally (sort of) got it. At the time I knew nothing about opendir
or readdir, though I understood glob a bit. I ended up writing:
walktree('d:/perl'); sub walktree { print "$_[0]\n"; # or real-world stuff here for ( glob("$_[0]/*") ) { -d && walktree($_); } }

or these alternatives:

for (grep(-d, glob("$_[0]/*"))) { walktree($_); } walktree($_) for (grep(-d, glob("$_[0]/*")));
To my amazement, they worked, though I'm still not entirely sure why.
I have since found that ways of doing it with opendir and readdir execute
much faster. But, execution speed aside: is there anything massively
dumb (or, worst of all, deprecated :) about using glob this way?

Replies are listed 'Best First'.
Re: More on directory recursion
by Ovid (Cardinal) on Jun 12, 2000 at 22:40 UTC
    I don't know much about this, but there is apparently a security issue here, so I'd like to extend this question a bit. On page 321 of the Perl Cookbook, it states
      Perl's built-in glob and <WILDCARD> notation ... currently use an external program to get the list of filenames on most systems.
    (English purists like myself will cringe at the placement of the last prepositional phrase, but that's another issue :).

    The Holy Cookbook further states that

      The use of the shell on Unix also makes this inappropriate for setuid scripts.
    While I have seen warnings about setuid and setgid scripts, I haven't had the opportunity to write any yet, so I am not aware of the security issues here. Enlightenment would put me a step closer to Perl nerdvana :)
      Pre-5.6 glob forks a shell and lets csh do the filename expansion. Allowing users to pass arbitrary strings to the shell is a Bad Thing, especially when you're running as root.

      5.6, however, implements glob internally through File::Glob, so it's a lot safer (though that feature is currently labelled "experimental").

      Update: This snippet from perlop should make it clear what is going on with glob on pre-5.6 Perl:

      while (<*.c>) { chmod 0644, $_; } is equivalent to open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|"); while (<FOO>) { chop; chmod 0644, $_; } In fact, it's currently implemented that way. (Which means it will not work on filenames with spaces in them unless you have csh(1) on your machine.)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://17743]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2022-10-06 20:39 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (28 votes). Check out past polls.