in reply to copy files

my $dir = @directory;

Evaluating an array as a scalar returns the number of elements in the array, so $dir will always get set to the number of elements in @directory, rather than the contents of @directory. You probably want something like my $dir = shift @directory; instead.