There's no special modifier in Perl, so doing a closure as you are doing is the best way. $memory can only be accessed by test(), so it's identical to static vars in C and (I presume) in VB.
Comment on Re: Making a variable in a sub retain its value between calls