my $r = shift; $r->register_cleanup(\&do_stuff); sub do_cleanup{ #some clean-up code here };