doy / try-tiny

minimal try/catch with proper preservation of $@
http://metacpan.org/release/Try-Tiny
10 stars 15 forks source link

Request to add retry() feature to Try::Tiny #2

Closed cobber closed 11 years ago

cobber commented 12 years ago

Hi Jesse,

I'd like to add a "retry" function to Try::Tiny.

My inspiration came while reworking the error handling in one of my projects. I kept ending up with the following pattern:

RETRY:
try {
    ...
} catch {
    # check what might have caused the error
    # try to fix what broke
    goto RETRY if $try_again;
};

Ugly!

This patch makes it possible to simply call retry:

try {
    ...
} catch {
    # check what might have caused the error
    # try to fix what broke
    retry if $try_again;
};

It works much like skip() in Test::More - but doesn't require the user to set up a label and it uses perl's redo function to unwind the stack properly.

I hope you like it!

Steve

doy commented 11 years ago

I don't think this is worth adding to Try::Tiny itself (it's a bit of an edge case). It wouldn't be hard to write a wrapper module that did this though, and I think that's where this functionality would belong.