in reply to accessing $var problem due to package and subs
Lexical (my) variables aren't accessible outside of the curlies or file they are in. Use package variables instead. You probably want to use local in addition to our to prevent the clobbering of any existing value.
use strict ; use warnings ; package FOO ; sub Principal::bof { our $fic; # Save us from typing $FOO::fic. local $fic = "toto"; # 'local' restores $fic when we exit this sub # The above can be combined into: # local our $fic = "toto"; bof2(); } sub bof2 { our $fic; # Save us from typgin $FOO::fic. print $fic; } 1;
our doesn't create the variable if it already exists. our simply disables use strict 'vars' for the specified var within the curlies it is in. The variable continues to exist outside of the curlies.
|
|---|