| 1 | package Variable::Disposition; | ||||||||||
| 2 | # ABSTRACT: dispose of variables | ||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | use parent qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | our $VERSION = '0.004'; | |||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Variable::Disposition - helper functions for disposing of variables | ||||||||||
| 13 | |||||||||||
| 14 | =head1 VERSION | ||||||||||
| 15 | |||||||||||
| 16 | version 0.004 | ||||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | use feature qw(say); | ||||||||||
| 21 | use Variable::Disposition; | ||||||||||
| 22 | my $x = []; | ||||||||||
| 23 | dispose $x; | ||||||||||
| 24 | say '$x is no longer defined'; | ||||||||||
| 25 | |||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||
| 27 | |||||||||||
| 28 | Provides some basic helper functions for making sure variables go away | ||||||||||
| 29 | when you want them to. | ||||||||||
| 30 | |||||||||||
| 31 | Currently provides L</dispose> as a default import. To avoid this: | ||||||||||
| 32 | |||||||||||
| 33 | use Variable::Disposition (); | ||||||||||
| 34 | |||||||||||
| 35 | In addition, L</retain> and L</retain_future> are available as optional | ||||||||||
| 36 | imports. | ||||||||||
| 37 | |||||||||||
| 38 | use Variable::Disposition qw(dispose retain retain_future); | ||||||||||
| 39 | |||||||||||
| 40 | The C< :all > tag can be used to import every available function: | ||||||||||
| 41 | |||||||||||
| 42 | use Variable::Disposition qw(:all); | ||||||||||
| 43 | |||||||||||
| 44 | but it would be safer to use a version instead: | ||||||||||
| 45 | |||||||||||
| 46 | use Variable::Disposition qw(:v1); | ||||||||||
| 47 | |||||||||||
| 48 | since these are guaranteed not to change in future. | ||||||||||
| 49 | |||||||||||
| 50 | Other functions for use with L<Future> and L<IO::Async> are likely to be | ||||||||||
| 51 | added later. | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | 4 | our @EXPORT_OK = qw(dispose retain retain_future); | |||||||||
| 56 | |||||||||||
| 57 | 4 | our %EXPORT_TAGS = ( | |||||||||
| 58 | all => [ @EXPORT_OK ], | ||||||||||
| 59 | v1 => [ qw(dispose retain retain_future) ], | ||||||||||
| 60 | ); | ||||||||||
| 61 | |||||||||||
| 62 | 4 | our @EXPORT = qw(dispose); | |||||||||
| 63 | |||||||||||
| 64 | 4 | ▶ | use Scalar::Util (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 65 | |||||||||||
| 66 | 4 | our %RETAINED; | |||||||||
| 67 | |||||||||||
| 68 | =head1 FUNCTIONS | ||||||||||
| 69 | |||||||||||
| 70 | =cut | ||||||||||
| 71 | |||||||||||
| 72 | =head2 dispose | ||||||||||
| 73 | |||||||||||
| 74 | Undefines the given variable, then checks that the original ref was destroyed. | ||||||||||
| 75 | |||||||||||
| 76 | my $x = [1,2,3]; | ||||||||||
| 77 | dispose $x; | ||||||||||
| 78 | # $x is no longer defined. | ||||||||||
| 79 | |||||||||||
| 80 | This is primarily intended for cases where you no longer need a variable, and want | ||||||||||
| 81 | to ensure that you haven't accidentally captured a strong reference to it elsewhere. | ||||||||||
| 82 | |||||||||||
| 83 | Note that this clears the B<caller>'s variable. | ||||||||||
| 84 | |||||||||||
| 85 | This function is defined with a prototype of ($), since it is only intended for use | ||||||||||
| 86 | on scalar variables. To clear multiple variables, use a L<foreach> loop: | ||||||||||
| 87 | |||||||||||
| 88 | my ($x, $y, $z) = ...; | ||||||||||
| 89 | dispose $_ for $x, $y, $z; | ||||||||||
| 90 | is($x, undef); | ||||||||||
| 91 | is($y, undef); | ||||||||||
| 92 | is($z, undef); | ||||||||||
| 93 | |||||||||||
| 94 | =cut | ||||||||||
| 95 | |||||||||||
| 96 | sub dispose($) { | ||||||||||
| 97 | 10 | ▶ | die "Variable not defined" unless defined $_[0]; | ||||||||
|
Subroutine
dispose: called CC=4 SCAR=13.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 98 | 9 | ▶ | die "Variable was not a ref" unless ref $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 8 | delete $RETAINED{$_[0]}; # just in case we'd previously retained this one | |||||||||
| 100 | 8 | Scalar::Util::weaken(my $copy = $_[0]); | |||||||||
| 101 | 8 | undef $_[0]; | |||||||||
| 102 | 8 | ▶ | die "Variable was not released" if defined $copy; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | =head2 retain | ||||||||||
| 106 | |||||||||||
| 107 | Keeps a copy of this variable until program exit or L</dispose>. | ||||||||||
| 108 | |||||||||||
| 109 | Returns the original variable. | ||||||||||
| 110 | |||||||||||
| 111 | =cut | ||||||||||
| 112 | |||||||||||
| 113 | sub retain($) { | ||||||||||
| 114 | 1 | ▶ | die "Variable not defined" unless defined $_[0]; | ||||||||
|
Subroutine
retain: called CC=3 SCAR=11.4
Pod
documented
Branch: 1 branch
| |||||||||||
| 115 | 1 | ▶ | die "Variable was not a ref" unless ref $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 1 | $RETAINED{$_[0]} = $_[0]; | |||||||||
| 117 | 1 | $_[0] | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | =head2 retain_future | ||||||||||
| 121 | |||||||||||
| 122 | Holds a copy of the given L<Future> until it's marked ready, then releases our copy. | ||||||||||
| 123 | Does not use L</dispose>, since that could interfere with other callbacks attached | ||||||||||
| 124 | to the L<Future>. | ||||||||||
| 125 | |||||||||||
| 126 | Returns the original L<Future>. | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||
| 129 | |||||||||||
| 130 | sub retain_future { | ||||||||||
| 131 | 6 | ▶ | my ($f) = @_; | ||||||||
|
Subroutine
retain_future: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 132 | 6 | ▶ | die "Variable does not seem to be a Future, since it has no ->on_ready method" unless $f->can('on_ready'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 6 | ▶ | $f->on_ready(sub { undef $f }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 4 | 1; | |||||||||
| 137 | |||||||||||
| 138 | __END__ | ||||||||||