Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is the difference between my and local in Perl?
Ask Question. Asked 13 years, 1 month ago. Active 2 months ago. Viewed 43k times. Improve this question. Brian G Brian G Add a comment. Active Oldest Votes. This can be confusing at first, so consider the following example. Improve this answer. Jeremy Bourque Jeremy Bourque 3, 1 1 gold badge 20 20 silver badges 18 18 bronze badges. Dynamic Scoping. It is a neat concept. Many people don't use it, or understand it.
Venkata Raju 4, 2 2 gold badges 25 25 silver badges 32 32 bronze badges. You didn't call the subroutines. Don't conditionally declare lexical variables: it has undefined behavior. Quoting from Learning Perl : But local is misnamed, or at least misleadingly named. Drew Stephens Drew Stephens Aristotle Pagaltzis Aristotle Pagaltzis k 22 22 gold badges 95 95 silver badges 96 96 bronze badges. Word of warning: both of these articles are quite old, and the second one by the author's own warning is obsolete.
It demonstrates techniques for localization of file handles that have been superseded by lexical file handles in modern versions of Perl. Steve Jessop Steve Jessop k 34 34 gold badges silver badges bronze badges. Generally use my, it's faster and doesn't do anything kind of weird.
Eli Courtwright k 63 63 gold badges silver badges bronze badges. While this may be true, it's basically a side effect of the fact that "local"s are intended to be visible down the callstack, while "my"s are not. And while overriding the value of a global may be the main reason for using "local", there's no reason you can't use "local" to define a new variable. For example, try using local to define a variable when option explicit is enabled. You need to use "our" or "my" to define a new global or local variable.
Jesus did I really say option explicit to refer to the Perl feature. I meant obviously "use strict". From man perlsub : Unlike dynamic variables created by the local operator, lexical variables declared with my are totally hidden from the outside world, including any called subroutines. Michael Carman Michael Carman Look at the following code and its output to understand the difference. A package variable can also be qualified with its package name e. A lexical variable is associated with a block of code.
It lives in a pad and can only be accessed from within the block. It stores the existing value of the variable and gives it a new, undef , value. The previous value of the variable is restored when the innermost enclosing block of code is exited. The variable only exists until the innermost enclosing block of code is exited. This way you temporarily replace the value of settings, while perl resets them back to their original state afterwards.
I have a "my" variable that I want to update within an if.. I'm obviously overlooking something in either declaring the variable to be global, or some way to reference the variable directly. I just don't quite know where to look. TEST starts with double quotes and ends with a single quote. If I fix that, this works as expected for me. Comment on The difference between my and local Select or Download Code. Replies are listed 'Best First'. What's this "scope" they keep telling you about?
PerlMonks lovingly hand-crafted by Tim Vroom. Update: forgot that base handles fields if you are using the fields pragma , which parent doesn't do. As Dirk mentioned in his answer, there isn't actually a problem with the code that you posted. This messing is either explicit, using new.
The moral of the story is twofold. Firstly, try to avoid explicitly manipulating environments, unless you are really sure that you know what you are doing. And secondly, if a function has a data argument then put all the variables that you need the function to use inside that data frame.
Note however that this is absolutely no better in any regard whatsoever from the far more obvious:. Because you are not separating out your tokens for calling exec directly.
That would look more like this:. If your free check is something like df. What is the difference between my and local in Perl?
0コメント