in reply to special context of file names in array?

I know this is the same thing most people put initially, but have you tried:
use strict; use warnings;

Also have you opened the file TOCOUT for writing, anywhere?

also: I think the code

foreach my $filepage (@dirarray) {...};

or
foreach (@dirarray) { my $filepage = $_; .. }

instead of the $filepage=shift(@dirarray);

I hope this helps you along.

-Enlil