| 1 | package Sub::Attempts; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | #use warnings; | ||||||||||||||
| 5 | 5 | ▶ | use vars qw($VERSION @EXPORT); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 5 | $VERSION = "1.01"; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 5 | ▶ | use Exporter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 5 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 5 | @EXPORT = qw(attempts); | |||||||||||||
| 12 | 5 | ▶ | use Sub::Uplevel; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 5 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 NAME | ||||||||||||||
| 17 | |||||||||||||||
| 18 | Sub::Attempts - alter subroutines to try again on exceptions | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||
| 21 | |||||||||||||||
| 22 | use Sub::Attempts; | ||||||||||||||
| 23 | |||||||||||||||
| 24 | sub alter_db | ||||||||||||||
| 25 | { | ||||||||||||||
| 26 | my $dbh = DBI->connect("DBD::Mysql:foo", "mark", "opensaysme") | ||||||||||||||
| 27 | or die "Can't connect to database"; | ||||||||||||||
| 28 | $dbh->{RaiseException} = 1; | ||||||||||||||
| 29 | $dbh->do("alter table items change pie pies int(10)"); | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | # if there's a problem making pies, wait and try again | ||||||||||||||
| 33 | attempts("alter_db", tries => 3, delay => 2); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | |||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||
| 37 | |||||||||||||||
| 38 | Sometimes if a subroutine throws an exception the right thing to | ||||||||||||||
| 39 | do is wait for a while and then call the subroutine again, as the | ||||||||||||||
| 40 | error conditions that caused the subroutine to have to throw | ||||||||||||||
| 41 | the exception might have gone away. | ||||||||||||||
| 42 | |||||||||||||||
| 43 | This module exports one subroutine C<attempts> which can be used to | ||||||||||||||
| 44 | modifiy existing subroutines so that whenever that subroutine is | ||||||||||||||
| 45 | called it will be automatically be called again in the event that it | ||||||||||||||
| 46 | throws an exception. | ||||||||||||||
| 47 | |||||||||||||||
| 48 | use LWP::Simple qw(get); | ||||||||||||||
| 49 | sub journal_rss | ||||||||||||||
| 50 | { | ||||||||||||||
| 51 | return get("http://use.perl.org/~2shortplanks/journal/rss") | ||||||||||||||
| 52 | or die "Couldn't get journal"; | ||||||||||||||
| 53 | } | ||||||||||||||
| 54 | attempts("journal_rss"); | ||||||||||||||
| 55 | |||||||||||||||
| 56 | By default perl will attempt to run to run the subroutine again | ||||||||||||||
| 57 | without delay if an exception is thrown. If on the second run an | ||||||||||||||
| 58 | exception is again thrown, that exception will be propogated out of | ||||||||||||||
| 59 | the subroutine as normal. | ||||||||||||||
| 60 | |||||||||||||||
| 61 | The particulars of the subroutines re-execution can be changed by | ||||||||||||||
| 62 | passing extra parameters to C<attempts>. The C<tries> parameter | ||||||||||||||
| 63 | effects the number of times the subroutne will attempt to be executed. | ||||||||||||||
| 64 | The C<delay> parameter determines how long perl will wait - sleep - | ||||||||||||||
| 65 | in seconds (and fractions of a second) between execution attempts. | ||||||||||||||
| 66 | |||||||||||||||
| 67 | =head2 Methods | ||||||||||||||
| 68 | |||||||||||||||
| 69 | A method can be modified just like any other subroutine, provided the | ||||||||||||||
| 70 | subroutine defining the method is located in the same package as | ||||||||||||||
| 71 | C<attempts> is called from. If this is not the case (i.e. the method | ||||||||||||||
| 72 | is inherited and not overridden) then you should use the C<method> | ||||||||||||||
| 73 | parameter: | ||||||||||||||
| 74 | |||||||||||||||
| 75 | attempts("get_pie", tries => 3, method => 1); | ||||||||||||||
| 76 | |||||||||||||||
| 77 | This has the same effect as writing: | ||||||||||||||
| 78 | |||||||||||||||
| 79 | sub get_pie | ||||||||||||||
| 80 | { | ||||||||||||||
| 81 | my $self = shift; | ||||||||||||||
| 82 | $self->SUPER::get_pie(@_); | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | attempts("get_pie", tries => 3); | ||||||||||||||
| 86 | |||||||||||||||
| 87 | If a method is defined by a subroutine in the current package then | ||||||||||||||
| 88 | the C<method> parameter has no effect | ||||||||||||||
| 89 | |||||||||||||||
| 90 | =cut | ||||||||||||||
| 91 | |||||||||||||||
| 92 | sub attempts | ||||||||||||||
| 93 | 4 | ▶ | { | ||||||||||||
|
Subroutine
attempts: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 94 | # here be subroutine magic | ||||||||||||||
| 95 | 5 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 96 | |||||||||||||||
| 97 | 4 | my $subname = shift; | |||||||||||||
| 98 | 4 | my %args = @_; | |||||||||||||
| 99 | |||||||||||||||
| 100 | # get the ref to the existing subroutine | ||||||||||||||
| 101 | 4 | ▶ | my $package = caller || croak "Not in a package"; | ||||||||||||
|
Condition50%1/2
&CORE::GLOBAL::caller() || croak('Not in a package')
MC/DC0%0/1
&CORE::GLOBAL::caller() || croak('Not in a package')
&CORE::GLOBAL::caller()
Truth table50%1/2
A
A= &CORE::GLOBAL::caller()
| |||||||||||||||
| 102 | 4 | my $glob = \*{"${package}::${subname}"}; | |||||||||||||
| 103 | 4 | my $old_sub = *{ $glob }{CODE}; | |||||||||||||
| 104 | |||||||||||||||
| 105 | # is it a method? | ||||||||||||||
| 106 | 4 | ▶ | if (!defined($old_sub)) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | { | ||||||||||||||
| 108 | 1 | ▶ | if ($args{method}) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | { | ||||||||||||||
| 110 | # this eval is here as we need to switch packages to declare a | ||||||||||||||
| 111 | # subroutine so SUPER works and with the current limitations of | ||||||||||||||
| 112 | # perl, there's no way to do that by mucking about with | ||||||||||||||
| 113 | # typeglobs. | ||||||||||||||
| 114 | 1 | ▶ | eval qq{package $package; | ||||||||||||
|
Subroutine
foo: called CC=1 SCAR=0.0
| |||||||||||||||
| 115 | sub $subname | ||||||||||||||
| 116 | { | ||||||||||||||
| 117 | my \$this = shift; | ||||||||||||||
| 118 | \$this->SUPER::$subname(\@_) | ||||||||||||||
| 119 | } | ||||||||||||||
| 120 | }; | ||||||||||||||
| 121 | 1 | $old_sub = *{ $glob }{CODE}; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | else | ||||||||||||||
| 124 | { | ||||||||||||||
| 125 | 0 | croak "Can't wrap '$subname', doesn't exist in package '$package'" | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | # replace the subroutine | ||||||||||||||
| 130 | 4 | _attempts($old_sub, $glob, %args); | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | sub _attempts | ||||||||||||||
| 134 | 5 | ▶ | { | ||||||||||||
|
Subroutine
_attempts: called CC=1 SCAR=0.0
| |||||||||||||||
| 135 | # here be subroutine magic too | ||||||||||||||
| 136 | 5 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 137 | |||||||||||||||
| 138 | 5 | my $old_sub = shift; | |||||||||||||
| 139 | 5 | my $glob = shift; | |||||||||||||
| 140 | 5 | my %args = @_; | |||||||||||||
| 141 | |||||||||||||||
| 142 | # create a new subroutine that does the attempt stuff | ||||||||||||||
| 143 | my $sub = sub | ||||||||||||||
| 144 | 14 | ▶ | { | ||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||
| 145 | # find out how many attempts we're going to take, | ||||||||||||||
| 146 | # defaulting to two. | ||||||||||||||
| 147 | 14 | ▶ | my $tries = exists($args{tries}) ? $args{tries} : 2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | |||||||||||||||
| 149 | # do we want a list? | ||||||||||||||
| 150 | 14 | my $wantarray = wantarray; | |||||||||||||
| 151 | |||||||||||||||
| 152 | # try while we've got tries left. | ||||||||||||||
| 153 | 14 | while (1) | |||||||||||||
| 154 | { | ||||||||||||||
| 155 | 33 | my $result; | |||||||||||||
| 156 | my @results; | ||||||||||||||
| 157 | |||||||||||||||
| 158 | # try running the code | ||||||||||||||
| 159 | eval | ||||||||||||||
| 160 | 33 | { | |||||||||||||
| 161 | 33 | ▶ | if ($wantarray) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | 4 | { @results = uplevel 2, $old_sub, @_ } | |||||||||||||
| 163 | else | ||||||||||||||
| 164 | 29 | { $result = uplevel 2, $old_sub, @_ } | |||||||||||||
| 165 | }; | ||||||||||||||
| 166 | |||||||||||||||
| 167 | # return if we're sucessful | ||||||||||||||
| 168 | 33 | ▶ | return ($wantarray ? @results : $result ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 169 | unless $@; | ||||||||||||||
| 170 | |||||||||||||||
| 171 | # we've used up a try | ||||||||||||||
| 172 | 23 | $tries--; | |||||||||||||
| 173 | 23 | ▶ | last if $tries < 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | |||||||||||||||
| 175 | # sleep if we need to | ||||||||||||||
| 176 | select undef, undef, undef, $args{delay} | ||||||||||||||
| 177 | 19 | ▶ | if exists $args{delay}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | # got this far and didn't already return, so propogate the error | ||||||||||||||
| 181 | 4 | croak $@; | |||||||||||||
| 182 | 5 | }; | |||||||||||||
| 183 | |||||||||||||||
| 184 | # place the subroutine into the symbol table | ||||||||||||||
| 185 | 5 | *{ $glob } = $sub; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||
| 189 | |||||||||||||||
| 190 | Written by Mark Fowler E<lt>mark@twoshortplanks.comE<gt> | ||||||||||||||
| 191 | |||||||||||||||
| 192 | Copryright Mark Fowler 2003. All Rights Reserved. | ||||||||||||||
| 193 | |||||||||||||||
| 194 | This program is free software; you can redistribute it | ||||||||||||||
| 195 | and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 196 | |||||||||||||||
| 197 | =head1 BUGS | ||||||||||||||
| 198 | |||||||||||||||
| 199 | Though list and scalar context will be preserved for the call to the | ||||||||||||||
| 200 | original subroutine, other forms of context (as offered by B<Want>) | ||||||||||||||
| 201 | will be lost. Therefore, amongst other things, a subroutine modified | ||||||||||||||
| 202 | by C<attempts> cannot currently 1be used as a lvalue. | ||||||||||||||
| 203 | |||||||||||||||
| 204 | The caller bug is now defeated, thanks to B<Sub::Uplevel> things think | ||||||||||||||
| 205 | they're in a higher caller frame. | ||||||||||||||
| 206 | |||||||||||||||
| 207 | Bugs should be reported to me via the CPAN RT system. | ||||||||||||||
| 208 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub::Attempts>. | ||||||||||||||
| 209 | |||||||||||||||
| 210 | =head1 SEE ALSO | ||||||||||||||
| 211 | |||||||||||||||
| 212 | L<Attribute::Attempts>, | ||||||||||||||
| 213 | L<Attempt> | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =cut | ||||||||||||||
| 216 | |||||||||||||||
| 217 | 5 | 1; | |||||||||||||