Forum Discussion

Charles_Harris's avatar
Charles_Harris
Icon for Nimbostratus rankNimbostratus
Feb 12, 2007

Global 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

  • 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

     

  • So to put that in practice:

     

     

    when RULE_INIT {

     

    set ::hello "Why don't I show anything..."

     

    }

     

    when LB_SELECTED {

     

    log "$::hello"

     

    }
  • 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