Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: listing all subdirectories of directory into file

by sauoq (Abbot)
on May 25, 2012 at 22:17 UTC ( #972545=note: print w/ replies, xml ) Need Help??


in reply to Re: listing all subdirectories of directory into file
in thread listing all subdirectories of directory into file

A slightly simpler approach than jdrago999's above is to use these built-in perl functions to achieve your objective:

Do you think? I think File::Find would be the simpler approach... it does a lot for you. I'd do it something like this (untested):

#!/usr/bin/perl use warnings; use strict; use File::Find; use Cwd qw(abs_path); sub w { -d && print $File::Find::name,$/ } File::Find::find( {wanted=>\&w}, abs_path($_)) for @ARGV;

-sauoq
"My two cents aren't worth a dime.";


Comment on Re^2: listing all subdirectories of directory into file
Download Code
Re^3: listing all subdirectories of directory into file
by NetWallah (Abbot) on May 26, 2012 at 04:55 UTC
    I would agree that File::Find is more useful, more powerful, and more productive.

    However, it has some requirements/expectations of the user. It requires the user to understand callbacks, depth-first vs. width first, How to get the current directory name - all of which you can get if you RTFM, but IMHO, it is conceptually fairly difficult to ingest, for a newbie. That was my reasoning behind recommending 'opendir'. Also, once someone has gone through that pain, it is easier to appreciate a fine module like FF and FF:Rule.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      It requires the user to understand callbacks, depth-first vs. width first, How to get the current directory name

      Ah, but if you are going to roll it by hand, you should probably understand depth vs. breadth first searching. And certainly how to get the current directory name. You might also want to know a thing or two about symlinks and symlink loops, and think out your error handling. And, really, if you are going to do all that, there's a good chance you understand callbacks already. (BTW, I don't know how much you really need to understand about callbacks to follow an example and use one; not that I advocate using something that you don't understand... but you gotta learn somehow.)

      -sauoq
      "My two cents aren't worth a dime.";
Re^3: listing all subdirectories of directory into file
by aaron_baugher (Deacon) on May 26, 2012 at 16:39 UTC

    At first I thought File::Find might not be that good for this, because he wants to make a list within each directory of its subdirectories, which at first glance means "looking at" each directory twice: once when you're adding it to its parent directory's list, and then again when File::Find comes across it. But then I thought of this, which is very simple, but has the downside of opening the output file every time it needs to write a line:

    #!/usr/bin/env perl use Modern::Perl; use File::Find; sub w { if( -d $_ ){ open my $out, '>>', 'list' or die $!; print $out "$_\n"; close $out; } } File::Find::find(\&w, '.');

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      Why open and close $out every time?

      #!/usr/bin/env perl use Modern::Perl; use File::Find; # $out is used within w(): open my $out, '>>', 'list' or die $!; sub w { if( -d $_ ){ print $out "$_\n"; } } File::Find::find(\&w, '.');

        If you look at his spec, he wants more than one "out" file. In each directory, he wants a file named 'list' that contains a list of every subdirectory of that directory. So in a directory structure like the following, you would get 'list' files like so:

        $ find . -ls|cut -b14-24,68- drwxr-xr-x . drwxr-xr-x ./a -rw-r--r-- ./a/list drwxr-xr-x ./a/d drwxr-xr-x ./a/c drwxr-xr-x ./b -rw-r--r-- ./list $ cat list a b $ cat a/list c d

        Aaron B.
        Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-12-26 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (173 votes), past polls