in reply to Read a list of files into an array

This sub returns an array ref. You could also opt not to use the sub to access the array directly, instead.
#!/usr/bin/perl use strict; use warnings; my $somedirectory = "/root/dir/"; my $files = dirlist($someDirectory); foreach my $fileInDirectory (@$files) { dosomething($fileInDirectory); } sub dirlist { my $dir = shift; my @filelist; opendir(DIR, $dir) or die "can't opendir $dir: $!"; while (defined(my $file = readdir(DIR))) { # Skip "." and ".." next if ($file =~ /^\.\.?$/); push(@filelist, $file); } closedir(DIR); return \@filelist; }