| 1 | # | ||
| 2 | # Copyright (c) 2015-2019 Christian Jaeger, copying@christianjaeger.ch | ||
| 3 | # | ||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||
| 7 | # bundled with this file. | ||
| 8 | # | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Chj::Destructor | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | 1 | ▶ | use Chj::Destructor; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | my $z = 0; | ||
| 19 | { | ||
| 20 | my $x = ["foo", Destructor { $z++ }]; | ||
| 21 | } | ||
| 22 | is $z, 1; | ||
| 23 | |||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Util to help debug or test memory deallocation. | ||
| 28 | |||
| 29 | =head1 SEE ALSO | ||
| 30 | |||
| 31 | Implements: L<FP::Struct::Show>, L<FP::Abstract::Pure> | ||
| 32 | |||
| 33 | End.pm, but that one does not type-check the destructor argument | ||
| 34 | early, nor does it localize error variables in its DESTROY method. | ||
| 35 | |||
| 36 | =head1 NOTE | ||
| 37 | |||
| 38 | This is alpha software! Read the status section in the package README | ||
| 39 | or on the L<website|http://functional-perl.org/>. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | package Chj::Destructor; | ||
| 44 | 60 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | 60 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | 60 | ▶ | use warnings FATAL => 'uninitialized'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 47 | 60 | ▶ | use Exporter "import"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 48 | |||
| 49 | 60 | our @EXPORT = qw(Destructor); | |
| 50 | 60 | our @EXPORT_OK = qw(); | |
| 51 | 60 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |
| 52 | |||
| 53 | { | ||
| 54 | |||
| 55 | 60 | package Chj::_::Destructor; | |
| 56 | 60 | ▶ | use FP::Predicates ":all"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 57 | 60 | ▶ | use FP::Struct [[\&is_procedure, "thunk"]], 'FP::Struct::Show', |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 58 | 60 | 'FP::Abstract::Pure'; | |
| 59 | |||
| 60 | 4 | ▶ | sub DESTROY { |
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||
| 61 | 4 | my ($self) = @_; | |
| 62 | 4 | local ($@, $!, $?, $^E, $.); | |
| 63 | 4 | $self->thunk->() | |
| 64 | } | ||
| 65 | 60 | _END_ | |
| 66 | } | ||
| 67 | |||
| 68 | # Chj::_::Destructor::constructors->import -- no, special prototype: | ||
| 69 | 5 | ▶ | sub Destructor (&) { |
|
Subroutine
Destructor: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 70 | 5 | Chj::_::Destructor->new($_[0]) | |
| 71 | } | ||
| 72 | |||
| 73 | 60 | ▶ | use Chj::TEST; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 74 | |||
| 75 | 1 | ▶ | TEST { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 76 | 1 | my $z = 0; | |
| 77 | { | ||
| 78 | 1 | ▶ | my $x = ["foo", Destructor { $z++ }]; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 79 | } | ||
| 80 | $z | ||
| 81 | 1 | } | |
| 82 | 60 | 1; | |
| 83 | |||
| 84 | 60 | 1 | |