in reply to Re^2: Unable to declare local variable with "use strict".
in thread Unable to declare local variable with "use strict".

I think you should first make up your mind what kind of variables you would like to have. We should not use the term "local variable" in Perl, since it is not clear what this means. Perl has two type of variables: Lexical variables, and package global variables. Which one would you like to use?

Lexical variables are declared using my or state - the latter only if you are using Perl 5.10, and if feature 'state' is enabled.

Package global variables do not need to be declared. They can be declared using our. If you decide not to declare them, and have strict in effect (which is for sure a good idea), you have to qualify them with the full package name.

local is a function which operates on package global variables. Hence if you use it, and have strict in effect, you need to qualify the variable name with the package.

My impression is that a lot of the confusion about your posting arose, because you did not explain in the first place what kind of variable you want to use, so some people thought you are interested in lexicals (and suggested to use my), while others thought you want to have package globals (and suggested to use our).

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^4: Unable to declare local variable with "use strict".
by mr_p (Scribe) on Mar 29, 2010 at 14:17 UTC

    If possible I would like to use variable that is accessible bye ref_test2 without passing in as argument.

    sub ref_test1 { local our $type="connect"; print "Here in ref_test1.\n"; ref_test2(); } sub ref_test2 { our $type; print "Type: $type\n"; print "Here in ref_test2\n"; }
      If possible I would like to use variable that is accessible bye ref_test2 without passing in as argument.
      So you mean you want to share a variable between functions? The solution here is to use a lexical within a closure:

      { # closure starts here my $shared_variable; sub ref_test1 { ... } sub ref_test2 { ... } }

      -- 
      Ronald Fischer <ynnor@mm.st>
        Generally, when people speak of a 'closure' in Perl, people mean a specific kind of subroutine, not a block, as your comment seems to indicate.

        Closures are typically unnamed subroutines, holding a reference to their private instance of a variable (although such a variable may be shared between more than one subroutine). In your case, the subroutines are named, and there will only be once instance of $shared_variable.

        Note also that your technique doesn't scale very well. Suppose you have subroutines sub1, sub2, sub3 and sub4. sub1 and sub2 want to share a variable $var1, sub2 and sub3 want to share a variable $var2, sub3 and sub4 want to share variable $var3, and sub4 and sub1 want to share $var4. In effect, you end up with all variables visible to all subroutines. An extra block doesn't really buy you much.

Re^4: Unable to declare local variable with "use strict".
by JavaFan (Canon) on Mar 27, 2010 at 23:40 UTC
    We should not use the term "local variable" in Perl, since it is not clear what this means. Perl has two type of variables: Lexical variables, and package global variables.
    If the goal is to remain clear, talk about "lexical variables" (or lexically scoped variables) vs. "package variables". Don't go pretending that only package variables can be global.