sub localised { package localised; use Scalar::Util 'reftype'; my $ref = shift; my $reftype = reftype $ref or die "$ref is not a reference"; my @old = $reftype eq 'SCALAR' ? $$ref : $reftype eq 'ARRAY' ? @$ref : $reftype eq 'HASH' ? %$ref : die sprintf "Unsupported reftype %s", reftype $ref; return bless [$ref, @old]; sub DESTROY { my ($ref, @old) = @{+shift}; my $reftype = reftype $ref; $reftype eq 'SCALAR' ? ($$ref) : $reftype eq 'ARRAY' ? @$ref : %$ref = @old; } }