in reply to sub fails second time called

I recommend you use strict and warnings, first of all. If you run with warnings, it says:

defined(@array) is deprecated at ... (Maybe you should just omit the defined()?) 0 1 Use of uninitialized value in addition (+) at ... 1

If I remove the "defined", as suggested, it runs as desired.