in reply to when does the definition of a my variable take effect?

The problem with $result would have been found by use strict;.

The technique of allowing strict to find scoping problems has become second nature - catching bugs before they happen. Similarly, use warnings; can catch uninitialised variable bugs.