in reply to local & global function

I think that you intend to use two scalar variables, both named '$test', which you describe as 'global' and 'local'. That is possible, but probably not a good idea. Your code would be much clearer if you used different names for the two variables. Please post a version that works as intended. If there still is a good reason to use one name, we can address that later.
Bill