leoboy has asked for the wisdom of the Perl Monks concerning the following question:
I'm developping a tiny app with Redis (some kind of noSQL database) as storage back-end. Redis.pm (http://search.cpan.org/~melo/Redis-1.904/lib/Redis.pm) is fairly cool, however, I would like to protect Redis methods with eval{}, to be able to handle situations on which the Redis server disappears, and so on.
My first idea was to create a new class, let's say SRedis, that inherits from Redis :
As I'm a lazy boy, I just want to handle all original Redis methods with an AUTOLOAD method on my SRedis package. Code looks like this :package SRedis ; our @ISA = qw(Redis) ;
## Wonderful catch-all ## Input : Nothing ## Output : Asked data sub AUTOLOAD { # Input variables my ($instance_ref) = shift(@_) ; # Internal vars $tries = 0 ; # Determine initially called sub my $command = $AUTOLOAD ; # Format it to call corresponding method on Redis package $command =~ s/.*:// ; $command = 'SUPER::' . $command ; TRY:while($tries < $maxtries) { # Go on $errorcode = eval {@results = $instance_ref->$command(@_)} ; # Did it went well ? if(!defined($errorcode)) { # No - I should log something here # Update count $tries++ ; # Wait a little bit ? sleep(1) ; # Go back to work next TRY; } # Otherwise that's cool last TRY; } # Return data return (@results) ; }
My problem is that I don't know what kind of data the original command may return. Could be a scalar, an array or a hash. In this piece of code, I assume that the command returns an array.
How can I handle any kind of return in the eval, and make AUTOLOAD return it correctly to the initial caller ?
Thank you in advance for your advices.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to protect Redis calls with eval ?
by Tux (Canon) on Apr 27, 2011 at 15:39 UTC | |
by leoboy (Initiate) on Apr 28, 2011 at 19:37 UTC | |
|
Re: How to protect Redis calls with eval ?
by Anonymous Monk on Apr 27, 2011 at 11:26 UTC | |
by leoboy (Initiate) on Apr 27, 2011 at 14:27 UTC | |
|
Re: How to protect Redis calls with eval ?
by shmem (Chancellor) on Apr 27, 2011 at 15:10 UTC |