Forum Discussion

juan_baptiste_4's avatar
juan_baptiste_4
Icon for Nimbostratus rankNimbostratus
Oct 14, 2009

Define custom function (command ?)

Hi,

 

 

I have an irule that needs to execute some code in two different events, so to not have to put the same code twice in each event I would like to put that code in a function that I can call in both events, but I don't have a clue of how to do that. For what I have read, functions in tcl are called commands, but I can't find a reference about how define one. I tried using the proc keyword but when trying to add the irule it throws up an error of "undefined procedure: my_proc". I defined like:

 

 

proc write_log{} {

 

 

...

 

 

}

 

 

and then call it like:

 

 

write_log

 

 

On the events I need it. Can anyone give a hint or point me to the right documentation ?

 

 

Thanks
  • proc's are disabled from within iRules due to performance issues with their blocking nature. If you want to use a set of code that functions on two events, your best bet is to write it twice. Another option is to define the TCL code in a global variable and then use the TCL eval command to execute it. This bypasses any of the load time error checking so use it at your own risk.

     

     

     when HTTP_REQUEST { 
       set code {log local0. "this is executed from an eval statement"} 
       set error [catch { eval $code; } errVar]; 
       if { $error } { 
         log local0. "Error in code execution: $errVar"; 
       } 
     }

     

     

    Hope this helps...

     

     

    -Joe
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    There is an existing RFE (CR96170) described in this thread:

     

     

    Support for user-defined functions?

     

    http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&tpage=1&view=topic&postid=21564

     

     

    You could open a case with F5 and ask them to add your case to the Request For Enhancement.

     

     

    And that's a novel idea, Joe.

     

     

    Aaron