in reply to Listing of files in its sub-folders

Your grep is useless in it's current form. You could as well say my @contents = readdir DIR;

Change that to

my @contents = grep !/^\.\.?$/, readdir DIR;

to exclude the current and parent directory entries.

Then in your foreach loop, test whether a given entry is a directory. See -X. If it is a directory, open it and get the files in there (as you did with the current).

But you could also use File::Find from CPAN.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}