Perl

Started by
2 comments, last by Strife 22 years ago
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
Advertisement
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.
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).


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

This topic is closed to new replies.

Advertisement