Guest User

Untitled

a guest
Jun 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. package SourceCarp;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. sub import
  7. {
  8. my($class, %args) = @_;
  9.  
  10. $SIG{__DIE__} = sub { report(shift, 2); exit } if $args{fatal};
  11. $SIG{__WARN__} = \&report if $args{warnings};
  12. }
  13.  
  14. sub report
  15. {
  16. my($message, $level) = @_;
  17. $level ||= 1;
  18.  
  19. my($filename, $line) = (caller($level - 1))[1, 2];
  20. warn $message, show_source($filename, $line);
  21. }
  22.  
  23. sub show_source
  24. {
  25. my ($filename, $line) = @_;
  26. return '' unless open(my $fh, $filename);
  27.  
  28. my $start = $line - 4;
  29. my $end = $line + 4;
  30.  
  31. local $.;
  32. my @text;
  33. while(<$fh>)
  34. {
  35. next unless $. >= $start;
  36. last if $. > $end;
  37. my $highlight = $. == $line ? '* ' : ' ';
  38. push @text, sprintf("%s%04d: %s", $highlight, $., $_);
  39. }
  40.  
  41. return join('', @text, "\n");
  42. }
  43.  
  44. 1;
Add Comment
Please, Sign In to add comment