Lexical variables continued
Usually, if a lexical variable exists in an outer scope and you assign to it, the value is overwritten:
$something = "I'm giving up on you" func saySomething { $something = "Another message" say($something) } # Right now, $something is still the original value say($something) # "I'm giving up on you" # But now it's "Another message." Even out here. saySomething() # "Another message" say($something) # "Another message"
However, if you explicitly re-initialize the variable with
var
, it becomes local to the
innermost scope.
$something = "I'm giving up on you" func saySomething { var $something = "Another message" say($something) } saySomething() # Spits out "Another message" say($something) # Spits out "I'm giving up on you"
var
is only ever required in these cases. Any other time,
the first assignment of a new lexical variable has the same effect
as declaring it explicitly with var
.