The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Variable::OnDestruct::Scoped - Call a subroutine on destruction of a variable.

VERSION

version 0.002

SYNOPSIS

 use Variable::OnDestruct::Scoped;

 my @handle = on_destruct $var, sub { do_something() };
 push @handle, on_destruct @array, sub { do_something_else() };
 push @handle, on_destruct %array, sub { hashes_work_too() };
 push @handle, on_destruct &$sub, sub { so_do_closures($but_not_normal_subs) };
 push @handle, on_destruct *$glob, sub { and_even_globs($similar_caveats_as_subs_though) };
 
 @handle = () if $want_to_cancel_destructor;

DESCRIPTION

This module allows you to let a function be called when a variable gets destroyed. The destructor will work not only on scalars but also on arrays, hashes, subs and globs. For the latter two you should realize that most of them aren't scoped like normal variables. Subs for example will only work like you expect them to when they are closures (otherwise they're immortal).

FUNCTIONS

on_destruct $variable, \⊂

This function adds a destructor callback to a variable. This callback will be called when the variable is destroyed, but only if the canary it returns is still alive (meaning it's stored somewhere). If the canary is destructed first the callback will not be called. This function is exported by default.

SEE ALSO

AUTHOR

Leon Timmermans <fawaka@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Leon Timmermans.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.