Forum Discussion

Adam_24063's avatar
Adam_24063
Icon for Nimbostratus rankNimbostratus
Apr 15, 2007

the scope of a variable.

Hello all.

 

 

I have some questions I couldn't find answers to in the docs or the archives.

 

 

What is the scope of non-global variables declared in an iRule?

 

 

Specifically, in examples i've seen situations where a variable is set in one event handler and then read in another within the same rule which leads me to believe there's a per connection context that is implicitly available.

 

 

For example, in the HTTPSession limit example, there is something like this:

 

 

when HTTP_REQUEST {

 

set need_cookie 0

 

}

 

 

when HTTP_RESPONSE {

 

if {$need_cookie == 1} { ... }

 

}

 

 

My most specific question, would this variable still be available in the CLIENT_CLOSE handler?

 

 

As a corollary, does this mean that if i have some HTML, (say for error pages) that i store in a variable that's defined in RULE_INIT that there's a copy of this hanging around for every connection ?

 

 

Now that I've laid it out in this posting I'm starting to think that the answer to all these questions is "yes", but any clarification (or pointers to docs) would be appreciated.

 

 

Thanks,

 

Radu

3 Replies

  • I think it depends on whether or not an irule, or multiple irules are assigned to a VS. If so, I believe the answer is yes, depending on what you are setting, if it's a simple variable declaration (e.g. set my_var 1) then that would be readable by any event. However if you set (set my_var [HTTP::host]) that variable would not be available for say a client accept event.

     

     

    But I am only speculating.
  • I believe that variables declared in the RULE_INIT event are global by default. Variables declared in any other event with 'set my_local_var' are local and are accessible in every other event for that connection, but not accessible for any other connection. Variables declared in any other event with 'set ::my_global_var' are global and can be accessed in every other event and every other connection.

     

     

    Check Joe's post here for some additional info:

     

    Click here

     

     

    Aaron
  • Thanks for the info and the pointer.

     

     

    To clarify one thing:

     

     

    I believe that variables declared in the RULE_INIT event are global by default.

     

     

     

     

    The message you point out emphasizes that non global variables variables in RULE_INIT are NOT seen in other event handlers. (which makes sense since RULE_INIT isn't run on a per-connection basis).