Forum Discussion
Charles_Harris
Nimbostratus
Feb 12, 2007Global Variables in RULE_INIT
Hi,
This is a little strange, but I can't get a global variable defined within an iRule.
Have I got Monday syndrome or does this look odd for example:
when RULE_INIT{
set hello "Why don't I show anything..."
}
when LB_SELECTED{
log "$hello"
}
I just get errors in the LTM log like " - can't read "hello": no such variable"
Any ideas? or have I lost the plot?
Cheers,
-=ChaZ=-
5 Replies
- Joe_D_Rozario_4
Nimbostratus
I don't see any :: in the code !! Try using it. - James_Thomson
Employee
This thread has an example of setting and using a global variable:
http://devcentral.f5.com/Default.aspx?tabid=28&view=topic&forumid=5&postid=10449 - JRahm
Admin
So to put that in practice:
when RULE_INIT {
set ::hello "Why don't I show anything..."
}
when LB_SELECTED {
log "$::hello"
} - Charles_Harris
Nimbostratus
Thanks everyone! - Monday or just general stupidity on my part?! (the latter is more likely!).
Cheers! - Just to chime in here for a minute. Variables can be shared across a connection without being in the global scope (ie, using "::"). RULE_INIT is a special rule that is only run at initialization time and not for each connection. So, any local variables created there will not be available across other events. Any variables you want set in RULE_INIT make sure that you use the global scope.
Just keep in mind that if you are modifying global variables in different connections, they will be modified for all connections. One common mistake it to put SSL certificate info in a global variable and then trying to access it in another event. Each new connection will overwrite that global variable and will likely yield unwanted results.
Also, global variables do use up memory and are not ever freed unless you explicitly "unset" them. Connection variables are freed when the connection closes so there is a built-in garbage collection.
-Joe
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects
