Archived

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

Strife

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 this post


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


#!/usr/bin/perl -w

my @files = </dir/name/*>;
foreach $f(@files) {
print $f, "\n";
}


or


#!/usr/bin/perl -w
opendir(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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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!

rm -rf /bin/laden

Share this post


Link to post
Share on other sites