in reply to Re: Re: Variable scoping outside subs
in thread Variable scoping outside subs

I agree the behavior is strange. It seems that Perl is processing the declaration at compile-time, which is why the variable exists when the sub is declared, but isn't processing the assignment until that line is reached in runtime, which never happens.

Perhaps you've touched on an area where the right thing to do is ambiguous, so Perl's idea of what to do differs from yours.

Several people have already shown you ways to make this work, by making sure the my statement precedes the call to test in both compile- and run-time.