Assuming you're using strict (and you should be ), the variables need to be declared before they're used. Preferrably being completely encapsulated within the same block that uses/relies on them. In your code snippet,

$string, @array and %hash aren't being used. But assuming that you'll be using these 3 variables only within the whatever sub, where you declared them is perfect. Perl's garbage collection system will destroy these variables after the whatever sub terminates.

However, if you're using $string, @array and %hash only inside the for loop, use this instead: