How does the SVN :: Error callback identify the context from which it was called?

I wrote fairly extensive Perl modules and scripts using Perl SVN :: Client bindings , etc. Since SVN :: Client calls are in the back of the module, I redefined the default error handling.

So far I have done this by setting

$SVN::Error::handler = undef;

as described in the docs , but this makes the individual calls a bit messy, because you have to remember that each call is SVN::Clientin the context of the list and check the first value for errors.

I would like to switch to using the error handler that I would write; but $SVN::Error::handleris global, so I see no way that my callback can determine where the error came from and which object to set the error code for.

I was wondering if I can use the pool for this purpose: so far I have ignored the pools as irrelevant to working in Perl, but if I call the method SVN::Clientwith the pool I created, will the SVN :: Error object be created in that same bullet?

Does anyone have any knowledge or experience that is on this?

+3
source share
1 answer

, , , () - , (b) ( ) - , . , , - :

#
# This part is the library that implements error handling a bit like
# SVN::Client
#
sub default_error_handler {
  croak "An error occurred: $_[0]";
}

our $global_error_handler = \&default_error_handler;

sub library_function_that_might_fail {
  &$global_error_handler("Guess what - it failed!");
}

#
# This part is the function that wants to detect an error
#
sub do_lots_of_stuff {
  my $error = undef; # No errors so far!

  local($global_error_handler) = sub { $error = $_[0]; };

  library_function_that_might_fail();
  library_function_that_might_fail();
  library_function_that_might_fail();

  if ($error) {
    print "There was an error: $error\n";
  }
}


#
# Main program
#
do_lots_of_stuff();

, , do_lots_of_stuff() , - , $error .

+1

Source: https://habr.com/ru/post/1741131/


All Articles