| 1 | package Attempt; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 5 | ▶ | use Exporter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 5 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 5 | ▶ | use vars qw(@EXPORT $VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 5 | @EXPORT = qw(attempt); | |||||||||||||
| 8 | |||||||||||||||
| 9 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | #use warnings; | ||||||||||||||
| 11 | |||||||||||||||
| 12 | 5 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 5 | $VERSION = "1.01"; | |||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 NAME | ||||||||||||||
| 17 | |||||||||||||||
| 18 | Attempt - attempt to run code multiple times | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||
| 21 | |||||||||||||||
| 22 | use Attempt; | ||||||||||||||
| 23 | |||||||||||||||
| 24 | # if the database goes away while we're using it, just try again... | ||||||||||||||
| 25 | attempt { | ||||||||||||||
| 26 | |||||||||||||||
| 27 | my $dbh = DBI->connect("DBD::Mysql:foo", "mark", "opensaysme") | ||||||||||||||
| 28 | or die "Can't connect to database"; | ||||||||||||||
| 29 | $dbh->{RaiseException} = 1; | ||||||||||||||
| 30 | $dbh->do("alter table items change pie pies int(10)"); | ||||||||||||||
| 31 | |||||||||||||||
| 32 | } tries => 3, delay => 2; | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||
| 35 | |||||||||||||||
| 36 | Attempt creates a new construct for things that you might want to | ||||||||||||||
| 37 | attempt to do more than one time if they throw exceptions, because the | ||||||||||||||
| 38 | problems they throw exceptions to report might go away. | ||||||||||||||
| 39 | |||||||||||||||
| 40 | Exports a new construct called C<attempt>. The simplest way to use | ||||||||||||||
| 41 | C<attempt> is to write attempt followed by a block of code to attempt | ||||||||||||||
| 42 | to run. | ||||||||||||||
| 43 | |||||||||||||||
| 44 | attempt { | ||||||||||||||
| 45 | something_that_might_die(); | ||||||||||||||
| 46 | }; | ||||||||||||||
| 47 | |||||||||||||||
| 48 | By default perl will attempt to run to run the code again without | ||||||||||||||
| 49 | delay if an exception is thrown. If on the second run an exception | ||||||||||||||
| 50 | is again thrown, that exception will be propogated out of the attempt | ||||||||||||||
| 51 | block as normal. | ||||||||||||||
| 52 | |||||||||||||||
| 53 | The particulars of the run can be changed by passing parameters after | ||||||||||||||
| 54 | the code block. The C<tries> parameter effects the number of times the | ||||||||||||||
| 55 | code will attempt to be run. The C<delay> parameter determines how | ||||||||||||||
| 56 | often perl will wait - sleep - between runs. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | C<attempt> can return values, and you can exit out of an attempt block | ||||||||||||||
| 59 | at any point with a return statement as you might expect. List and | ||||||||||||||
| 60 | scalar context is preserved though-out the call to the block. | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =cut | ||||||||||||||
| 63 | |||||||||||||||
| 64 | sub attempt (&;@) | ||||||||||||||
| 65 | 8 | ▶ | { | ||||||||||||
|
Subroutine
attempt: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||
| 66 | 8 | my $code = shift; | |||||||||||||
| 67 | 8 | my %args = @_; | |||||||||||||
| 68 | |||||||||||||||
| 69 | 8 | my @results; | |||||||||||||
| 70 | my $result; | ||||||||||||||
| 71 | |||||||||||||||
| 72 | # find out how many attempts we're going to take, | ||||||||||||||
| 73 | # defaulting to two. | ||||||||||||||
| 74 | 8 | ▶ | my $tries = exists($args{tries}) ? $args{tries} : 2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | # try while we've got tries left. | ||||||||||||||
| 77 | 8 | while (1) | |||||||||||||
| 78 | { | ||||||||||||||
| 79 | # do we want a list? | ||||||||||||||
| 80 | 18 | my $wantarray = wantarray; | |||||||||||||
| 81 | |||||||||||||||
| 82 | # try running the code | ||||||||||||||
| 83 | eval | ||||||||||||||
| 84 | 18 | { | |||||||||||||
| 85 | 18 | ▶ | if ($wantarray) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 1 | { @results = $code->() } | |||||||||||||
| 87 | else | ||||||||||||||
| 88 | 17 | { $result = $code->() } | |||||||||||||
| 89 | }; | ||||||||||||||
| 90 | |||||||||||||||
| 91 | # return if we're sucessful | ||||||||||||||
| 92 | 18 | ▶ | return ($wantarray ? @results : $result ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 93 | unless $@; | ||||||||||||||
| 94 | |||||||||||||||
| 95 | # we've used up a try | ||||||||||||||
| 96 | 11 | $tries--; | |||||||||||||
| 97 | 11 | ▶ | last if $tries < 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | |||||||||||||||
| 99 | # sleep if we need to | ||||||||||||||
| 100 | select undef, undef, undef, $args{delay} | ||||||||||||||
| 101 | 10 | ▶ | if exists $args{delay}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | # got this far and didn't already return, so propogate the error | ||||||||||||||
| 105 | 1 | croak $@; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head1 AUTHOR | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Written by Mark Fowler E<lt>mark@twoshortplanks.comE<gt> | ||||||||||||||
| 111 | |||||||||||||||
| 112 | Copryright Mark Fowler 2003. All Rights Reserved. | ||||||||||||||
| 113 | |||||||||||||||
| 114 | This program is free software; you can redistribute it | ||||||||||||||
| 115 | and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 116 | |||||||||||||||
| 117 | =head1 BUGS | ||||||||||||||
| 118 | |||||||||||||||
| 119 | Only respects list and scalar context, doesn't replicated | ||||||||||||||
| 120 | the more complicated forms of context that B<Want> supports. | ||||||||||||||
| 121 | |||||||||||||||
| 122 | The caller isn't what you might expect from within the attempt | ||||||||||||||
| 123 | block (or rather, it is, but isn't what it would have been if | ||||||||||||||
| 124 | the block wasn't there) | ||||||||||||||
| 125 | |||||||||||||||
| 126 | Bugs should be reported to me via the CPAN RT system. | ||||||||||||||
| 127 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Attempt>. | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =head1 SEE ALSO | ||||||||||||||
| 130 | |||||||||||||||
| 131 | L<Sub::Attempts>, L<Attribute::Attempts> | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =cut | ||||||||||||||
| 134 | |||||||||||||||
| 135 | 5 | 1; | |||||||||||||