How do you handle a null variable in an irule?

I am using the below logic:

if { [string length $sort_dest_pool] > 0 } {
        set dest_pool [string trimleft $sort_dest_pool "$loc"]
    } else {
        set dest_pool $orig_pool
    }

If $sort_dest_pool is not defined, I get the following error/event:

can't read "sort_dest_pool": no such variable while executing "string length $sort_dest_pool"

According to this error, it is failing while trying to get the string length. This method was suggested as the solution for testing a string to see if it is set. Any ideas?

is info exists usable?

info exists

http://wiki.tcl.tk/1185

Unfortunately, now I am getting this error:

can't read "sort_dest_pool": no such variable while executing "info exists $sort_dest_pool"

Based on that man page you sent on that command, it should have worked. Thoughts?

can you try to remove $?

e.g.

% info exists t
0
% set t 1
1
% info exists t
1

can you try to remove $?

e.g.

% info exists t
0
% set t 1
1
% info exists t
1

That was the answer. Thanks!!

That was the answer. Thanks!!

Yep, you can use [info exists varname] to check if a variable is defined. Or you could set it to a null value by default and then check it later to see if it’s not null:

when HTTP_REQUEST {

    if {$some_condition == 1}{
        set sort_dest_pool "my_pool"
    } else {
        set sort_dest_pool ""
    }

     later...

    if {$sort_dest_pool eq ""}{
         Take some default action
    } else {
        pool $sort_dest_pool
    }
}

Aaron