| 1 | #!/usr/bin/perl -c | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Resource::Dispose; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Resource::Dispose - Syntax sugar for dispose pattern | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use Resource::Dispose; | ||||||||||||||||||||||||||||
| 12 | { | ||||||||||||||||||||||||||||
| 13 | resource my $obj = Some::Class->new; | ||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | # $obj->DISPOSE is called even if $obj can not be freed and destroyed | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | The dispose pattern is a design pattern which is used to handle resource | ||||||||||||||||||||||||||||
| 20 | cleanup in runtime environment that use automatic garbage collection. In Perl | ||||||||||||||||||||||||||||
| 21 | there is possibility that the object will be destructed during global | ||||||||||||||||||||||||||||
| 22 | destruction and it leads to memory leaking and other drawbacks like unclosed | ||||||||||||||||||||||||||||
| 23 | file handles, etc. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This module provides new keyword C<resource> as a syntax sugar for dispose | ||||||||||||||||||||||||||||
| 26 | pattern. The C<DISPOSE> method of the resource object is called if the | ||||||||||||||||||||||||||||
| 27 | resource is going out of scope. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =for readme stop | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | use Devel::Declare (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | use Guard (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 3 | our @CARP_NOT = qw(Devel::Declare); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 3 | my ($class) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | my $caller = caller; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | Devel::Declare->setup_for( | |||||||||||||||||||||||||||
| 54 | $caller, | ||||||||||||||||||||||||||||
| 55 | { resource => { const => \&parser } } | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | *{$caller.'::resource'} = sub ($) {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | return 1; | |||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 3 | our $Prefix = ''; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 48 | ▶ | sub get_linestr { | ||||||||||||||||||||||||||
|
Subroutine
get_linestr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 48 | return substr Devel::Declare::get_linestr, length $Prefix; | |||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 22 | ▶ | sub set_linestr { | ||||||||||||||||||||||||||
|
Subroutine
set_linestr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 22 | return Devel::Declare::set_linestr $Prefix . $_[0]; | |||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 12 | ▶ | sub parser { | ||||||||||||||||||||||||||
|
Subroutine
parser: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 12 | my ($keyword, $offset) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 12 | local $Prefix = substr get_linestr, 0, $offset; | |||||||||||||||||||||||||||
| 79 | 12 | strip_keyword(); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 12 | my $linestr = get_linestr; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 12 | ▶ | $linestr =~ s/\s*(?:(local|my|our|state)\s*)?(\$\w+|\(.*?\))// or croak 'Syntax error'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 10 | my ($scope, $decl) = ($1, $2); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 10 | my $before = ''; | |||||||||||||||||||||||||||
| 87 | 10 | ▶ | $before .= "$scope $decl; " if defined $scope; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 10 | ▶ | my @vars = $decl =~ /^\((.*)\)$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | ? split /\s*,\s*/, $1 | ||||||||||||||||||||||||||||
| 91 | : $decl; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 10 | foreach my $var (@vars) { | |||||||||||||||||||||||||||
| 94 | 12 | $before .= "Guard::scope_guard { $var->DISPOSE if eval { $var->can('DISPOSE') } }; "; | |||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 10 | ▶ | $before .= "$decl" if $linestr =~ /\s*=/ and $decl =~ /^\s*(\$\w+|\(\s*\$\w+\s*\))\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$linestr =~ /\s*=/ and $decl =~ /^\s*(\$\w+|\(\s*\$\w+\s*\))\s*$/
MC/DC100%2/2
$linestr =~ /\s*=/ and $decl =~ /^\s*(\$\w+|\(\s*\$\w+\s*\))\s*$/
$linestr =~ /\s*=/
$decl =~ /^\s*(\$\w+|\(\s*\$\w+\s*\))\s*$/
Truth table100%3/3
A and B
A= $linestr =~ /\s*=/B= $decl =~ /^\s*(\$\w+|\(\s*\$\w+\s*\))\s*$/
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 10 | set_linestr($before . $linestr); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 10 | return 1; | |||||||||||||||||||||||||||
| 102 | }; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 12 | ▶ | sub strip_space { | ||||||||||||||||||||||||||
|
Subroutine
strip_space: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 12 | my $skip = Devel::Declare::toke_skipspace length $Prefix; | |||||||||||||||||||||||||||
| 106 | 12 | set_linestr substr get_linestr, $skip; | |||||||||||||||||||||||||||
| 107 | 12 | return 1; | |||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 12 | ▶ | sub strip_keyword { | ||||||||||||||||||||||||||
|
Subroutine
strip_keyword: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 12 | strip_space; | |||||||||||||||||||||||||||
| 112 | 12 | ▶ | get_linestr =~ /^(resource)(?:\b|$)/ or croak 'Could not match resource keyword ', get_linestr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 12 | $Prefix .= $1; | |||||||||||||||||||||||||||
| 114 | 12 | return $1; | |||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | 1; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =for readme continue | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | This C<resource> keyword is inspired by C<using> keyword from C# language and | ||||||||||||||||||||||||||||
| 126 | extended C<try> keyword from Java 7 language. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | L<Guard>, L<Scope::Guard>, L<Devel::Declare>. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 BUGS | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | If you find the bug or want to implement new features, please report it at | ||||||||||||||||||||||||||||
| 133 | L<https://github.com/dex4er/perl-Resource-Dispose/issues> | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | The code repository is available at | ||||||||||||||||||||||||||||
| 136 | L<http://github.com/dex4er/perl-Resource-Dispose> | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Piotr Roszatycki <dexter@cpan.org> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Copyright (c) 2012 Piotr Roszatycki <dexter@cpan.org>. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 147 | the same terms as perl itself. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | See L<http://dev.perl.org/licenses/artistic.html> | ||||||||||||||||||||||||||||