in reply to PM and references

my $reg=shift; my $top=@$reg; print "START OF\n"; for(my $i=0;$i<=$top;$i++)

Use Perl iteration instead of a C style loop:

for my $data (@$reg) { ... }