• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Perl

## Recommended Posts

Ok, not exactly *nix related, but... Perl is weird. Useful and powerful, but weird. Anyway, is there a function similar to readdir that will let me read subdirectories rather than just files? Did I simply overlook it when trying to find it? Basically, I want to be able to scan a directory for all of its subdirectories and place them into an array. rm -rf /bin/laden

##### Share on other sites
Of course you can, it''s Perl. And like everything with Perl, there is more than one way to do it:

#!/usr/bin/perl -wmy @files = </dir/name/*>;foreach $f(@files) { print$f, "\n";}

or

#!/usr/bin/perl -wopendir(DIR, "/dir/name" or die;@allfiles = readdir(dir);$onefile = readdir(dir);foreach$f(@allfiles) {  print $f, "\n";}print "$onefile\n";closedir(DIR);

There are other, uglier ways too..

Hope this helps.

##### Share on other sites
oops must have done a type.

opendir( DIR, "/dir/name" ) or die;

And, of course, you''ll want to use either @allfiles = readdir(DIR) or \$onefile = readdir(DIR). IIRC Perl won''t rewind, so calling readdir after the @allfiles = readdir(DIR) will return an empty scalar (or ''undefined'', I''m not sure).

##### Share on other sites
Cool. I think from this experience, I''ve learned that I''m really rusty on my Perl. Anyway, I''ve gotten the script to read just the directories now, and I will now commence in coding the part to read each file and find the exec line... Creating the Fluxbox menus are trivial once I have that. Woohoo!

• ### Forum Statistics

• Total Topics
627716
• Total Posts
2978783

• 9
• 21
• 14
• 12
• 42