How to return an error message or a value


/ Published in: Perl
Save to your folder(s)

Courtesy of Michael Mathews


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.