|P is for Practical|
Aside from logic problems, you have a "scope" problem with your "@accum" variable.
You have declared it in the "while (my @accum=...)" statement, which makes it local to the while loop.
You attempt to use it outside the while loop (where it does not exist), in the "foreach" statement.
To correct the problem, the preferred method is to fix your logic.
If you do not understand what your logic problem is, use globals, and suffer later.
If you do not understand what I'm trying to say, please read up on variable scoping, and algorithms (these topics are fairly independent of the programming language you choose).
"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest." -Confucius