Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

scope - Local and Global variables in perl

I am having few doubts about the local/our scope in Perl. I read a lot of documentation, but I am still in confusion is there. Following are the confusions

  1. What is local scope?

    what I read is -> local copies the value of global variable, change the value, user will use it and outside the block it will retain the global value

    Confusion -> my does the same thing. Only benefit I see is that some variables like $package::var cannot be declared with my scope but can be declared with local scope. What else for local

  2. What is "global" variable?

What is read is -> Its scope is within the package. Basically we put the global variable in @EXPORT array and use it or append the namespace with it to use in other packages.

doubt -> Again if we declare variable with my scope in main only then we can access the variable throughout the package. Is that right? Is it possible to add the my scoped variables in @EXPORT array and use it in another packages?

I think global variables are declared with our keyword. Is there any other way to do so?

This question may look repetitive, but I am confused.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In terms of scoping, there are two kinds of variables in Perl.

  • Lexical variables are lexically scoped, which means they are only visible in the current lexical scope.
  • Package variables are globally scoped, which means they are visible by all code in the interpreter.

Here are ways to create variable.

  • my creates a lexical variable.
  • our creates a lexical variable that is aliased to the variable of the same name in the current package. In other words, our $foo; is the same as alias my $foo = $The::Current::Package::foo;.
  • Global variables are created on use.

local doesn't create any variables. It simply backs up a variable until the current lexical scope is destroyed.


my does the same thing.

No. local does not change the scope of a variable. While a lexical variable is only visible in a lexical scope, a localized global variable is still visible across the entire interpreter.

$x = 123;
sub foo { print "$x
"; }
{ local $x = 456; foo(); }  # 456
foo();                      # 123

$x = 123;
sub foo { print "$x
"; }
{ my $x = 456; foo(); }   # 123
foo();                    # 123

What else for local

local is primarily used to approximate the functionality of my for variables that cannot otherwise be declared lexically.

(Historically, that was all variables. Since 5.6, only punctuation variables cannot be declared lexically.)


What is "global" variable?

A variable that can seen globally, i.e. by any code in the interpreter.


Is it possible to add the my scoped variables in @EXPORT array and use it in another packages?

No. @EXPORT is used by Exporter. Exporter would not be able to find anything but global symbols (since files are compiled in fresh lexical scopes), so @EXPORT must only contain global symbols.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...