Is it ok to use Attribute::Handlers for implementing retry logic I have almost 50+ subroutine like verifyXXXX. I need to implement the retry logic for all these subs. I want to write this retry logic where the sub is actually implemented. If the return value of sub is false/undef then it will retry again. subs will be called in regular way, so that the caller will not know about the retry logic, something like.
verify_am_i_doing_good() or die('sorry you are not doing as expected.'); verify_am_i_fine() or die ('sorry you are not find.'); : :
the actual implementation of these functions is something like this in the package.
use Attribute::Handlers; use constant RETRY_LIMIT => 4; use constant RETRY_DELAY => 2; sub verify_am_i_doing_good : __retry { return 1 if ($x == $y); return; } sub __retry : ATTR(CODE) { my ($pkg, $sym, $code) = @_; my $name = *{ $sym }{NAME}; no warnings 'redefine'; *{ $sym } = sub { my $self = $_[0]; my $result; logMsg (INFO, "Executing subroutine $name with retry limit + " . RETRY_LIMIT); for (my $retryCount = 1; $retryCount <= RETRY_LIMIT; $retr +yCount++) { logMsg (INFO, "Executing subroutine $name with retry c +ount $retryCount"); my $result = $code->( @_ ); if ($result) { logMsg (INFO, "Expected result observed in retry c +ount $retryCount"); return wantarray ? @$result : $result; } else { logMsg (INFO, "Expected result is NOT observed in +retry count $retryCount"); logMsg (INFO, "Retrying again by updating uixml"); sleep RETRY_DELAY; $self->updateState(); } } logMsg (WARN, "Failed to verify expected result for subrou +tine $name with retry limit " . RETRY_LIMIT); return; }; }

The reason to use Attribute::Handlers, inplace of Attribute::Attempts is that in the case of failure, I need to call another subroutine `updateState()` before retrying (re-executing) the subroutine.

I got this idea of writing the retry logic from following post http://www.perl.com/pub/2007/04/12/lightning-four.html

My main concern is that since I am using this __retry attribute for almost 50+ subs. Is it a good practice to do in this way or is there anything simple I can do?

You help will be highly appreciated.

In reply to Is it ok to use `Attribute::Handlers` for implementing retry logic by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.