Difference between revisions of "Scope"

From Scriptwiki
Jump to: navigation, search
m
(Example: space before {)
 
(One intermediate revision by one other user not shown)
Line 21: Line 21:
  
  
%foo will now return bar EVERYWHERE.
+
%foo will now return bar EVERYWHERE (unless overridden by a local variable)
  
 
==Local Scope==
 
==Local Scope==
Line 44: Line 44:
  
  
 +
 +
==Scope Precedence==
 +
 +
A final note on scope: A local scope will take Precedence over a global scope. This means that if there is a global variable and a local variable with the same name, the local variable's value will be used.
 +
 +
===Example===
 +
 +
Suppose the following was called using /test and /test2
 +
 +
alias test {
 +
  set %foo bar.
 +
}
 +
 +
alias test2 {
 +
  var %foo = Vliedel Smells
 +
  [[echo]] -atg %foo
 +
  ; This will echo "Vliedel Smells" even though %foo is still a global variable with value "bar"
 +
  [[unset]] %foo
 +
  [[echo]] -atg %foo
 +
  ; This will echo "bar" because the first unset unset the local-scope variable %foo.
 +
}
  
 
==See Also==
 
==See Also==

Latest revision as of 00:07, 4 October 2009

Scope is the term used to refer to a variable's "life-span". mIRC variables have only two scopes.

Global Scope

A variable with a global scope is accessable everywhere in mIRC where a variable can be used. This means in the command line, in every script file, alias file, popup, etc. These variables are saved in the Variables tab of the mIRC Scripts editor (alt+r) which are saved to a file (usually vars.ini).

  • A global scoped variable is created with set and destroyed with unset

Example

Suppose the following was called, /test and then /test2.

alias test {
 set %foo bar.
; %foo is available here
}
alias test2 {
 set %bar foo
 ; %foo is available here after being set with /test
}


%foo will now return bar EVERYWHERE (unless overridden by a local variable)

Local Scope

A variable with a local scope is only available within the event, alias, or menu (popup) script in which it is used.

  • A local variable is created with var and is automatically destroyed after leaving scope (exiting the current alias, event or menu). It can also be destroyed by unset.

Example

Suppose the following was called using /test

alias test {
  var %foo = bar.
  ; %foo is available here
  test2 %foo
}
alias test2 {
 var %bar = foo
 ; %foo is NOT available here. We can, however, use $1 to refer to %foo because it was passed as a parameter to the alias.
}


Scope Precedence

A final note on scope: A local scope will take Precedence over a global scope. This means that if there is a global variable and a local variable with the same name, the local variable's value will be used.

Example

Suppose the following was called using /test and /test2

alias test {
  set %foo bar.
}
alias test2 {
  var %foo = Vliedel Smells
  echo -atg %foo
  ; This will echo "Vliedel Smells" even though %foo is still a global variable with value "bar"
  unset %foo
  echo -atg %foo
  ; This will echo "bar" because the first unset unset the local-scope variable %foo.
}

See Also

  • var - Command used to create a local variables
  • Local Variables - Variables with limited scope
  • set - Command used to create global variables