Posted By

wolfie on 11/13/06


Tagged

perl Error-handling


Versions (?)

How to return an error message or a value


 / Published in: Perl
 

Courtesy of Michael Mathews

  1. print one();
  2.  
  3. sub one {
  4. my $result = two();
  5.  
  6. if (ref $result eq 'ERROR') {
  7. die $result->{message};
  8. }
  9. else {
  10. return $result;
  11. }
  12. }
  13.  
  14. sub two {
  15. if (rand(100) > 50) {
  16. return bless {message=>"There is a problem here."}, 'ERROR';
  17. }
  18. return "Things are fine.";
  19. }

Report this snippet  

You need to login to post a comment.