ashish.kvarma has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am a novice to Moose and am stuck to a code which might be very trivial.

I am using handles to delegate few subroutines but I do want to have a common code for exception handling around them. My problem is that I want to throw different exceptions for different subroutines.

Please refer the code below:
has 'usr_manager' => ( is => 'rw', isa => 'UserManager', lazy_build => 1, handles => { list_users => 'list', load_user => 'load', }, ); has 'prod_manager' => ( is => 'rw', isa => 'ProductManager', lazy_build => 1, handles => { list_prods => 'list', load_prod => 'load', }, ); around qw(list_users load_user list_prods load_prod) => sub { my $orig = shift; my $self = shift; my $return_data; eval { $return_data = $self->$orig(@_); }; if (my $E = Exception::Class->caught('MyExeption')) { $E->rethrow(); } if ($@) { my $message = $self->truncate_err_msg($@); #Throw exception based on subroutine. } return $return_data; };
I tried passing extra parameter from handles (as in the example below) but its not it’s not passed to around.
has 'prod_manager' => ( is => 'rw', isa => 'ProductManager', lazy_build => 1, handles => { list_prods => ['list' => 'Execption1'], load_prod => ['load' => 'Execption2'], }, );

I can handle the exception inside the delegated subroutine but would like to do it at this one place as it changes will be at only one place.

Thanks in advance for any information/help you can provide.
Regards,
Ashish
  • Comment on Moose | How to identify subroutine inside an 'around' in case multiple subroutine share the same 'around'?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Moose | How to identify subroutine inside an 'around' in case multiple subroutine share the same 'around'?
by ikegami (Patriarch) on Aug 09, 2010 at 14:40 UTC
    Perl provides a means of attaching data to code. It's called a "closure".
    for (qw(list_users load_user list_prods load_prod)) { my $name = $_; around $name => sub { ... $name is available here too ... }; }
Re: Moose | How to identify subroutine inside an 'around' in case multiple subroutine share the same 'around'?
by Anonymous Monk on Aug 09, 2010 at 13:07 UTC

    I suggest that you consider either or both of two approaches:

    1. Define subclasses.   Override the “throw an exception” logic in each one.
    2. Define a single exception-type but allow for a parameter to be sent to the exception handling routine (or reliably retrieved from the object in question), so that a common exception-handling routine can accurately distinguish among the various cases.

    “Always keep your eye on the ball.”   Your objective is to be able to easily distinguish between the various objects and error cases; nothing more or less.   “TMTOWTDI!”   Don’t become too wedded to the first approach that pops into your head, then spend too much time trying to implement it.   Always be prepared to step back, to look at the objective afresh, and look for another equally-good way to get there.

      I am not very sure about the first point. If you meant sub-classing Exception then that's what I have. Different Exceptions (subclass of Exception) to throw for different subroutines.

      Regarding second point "allow for a parameter to be sent to the exception handling routine"; It has the same problem parameter will depend on subroutine. But you have a good point that it could be retrieved from the object itself (Why I didn't think it :-(. May be I was obsessed with the thought to get something out of Moose itself.).

      Regards,
      Ashish

        In the case of the first point:   the idea here is that, in order to raise an exception, the base-class will call one of its methods.   The descendant classes will then override those methods.   Hence, each descendant class can arrange to throw a particular Exception ... and/or to otherwise distinguish their response from everybody else’s.

        In the case of the second point:   well, it is extremely easy indeed, when faced with the obstacle of a thick iron door, to focus your entire attention upon how to drill through that door ... and, thus, to completely overlook the fact that the door is set into a wooden wall, next to an open window.   (Believe me, it happens to all of us.   That's why Homer Simpson is always whacking himself on the forehead and saying:   “Doh!!”)

Re: Moose | How to identify subroutine inside an 'around' in case multiple subroutine share the same 'around'?
by ashish.kvarma (Monk) on Aug 09, 2010 at 14:08 UTC

    One of my colleague suggested me to use caller to get the subroutine name. I have tried that, it does gives the subroutine name at level '2'.

    Even though it works, I am not very comfortable using it in code (Although its a Perl function but it looks like more of hack in code than a retrospection/Meta Data).

    Any suggestions on using it?

    Regards,
    Ashish