in reply to How do I stop this from being a never ending loop

You're continually expanding the size of @link (by autovivifying the element after the last). @link returns the size of the array, which is one higher than the last element of the array.

for ($i=0; $i <= @link; $i++)
should be
for (my $i=0; $i < @link; $i++)
or
for (my $i=0; $i <= $#link; $i++)

Even simpler would be
for my $i (0..$#link)
or even
for my $link (@link)

It's curious that @link (which contains multiple links) is singular and $links (a single link) is plural.