File Coverage

blib/lib/Parse/FSM/Error.pm
Criterion Covered Total %
statement 25 25 100.0
branch 10 10 100.0
condition 3 3 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1             # $Id: Lexer.pm,v 1.10 2013/07/27 00:34:39 Paulo Exp $
2            
3             package Parse::FSM::Error;
4            
5             #------------------------------------------------------------------------------
6            
7             =head1 NAME
8            
9             Parse::FSM::Error - Format error and waring messages
10            
11             =cut
12            
13             #------------------------------------------------------------------------------
14            
15 2     2   809 use 5.010;
  2         6  
16 2     2   8 use strict;
  2         4  
  2         37  
17 2     2   8 use warnings;
  2         4  
  2         101  
18            
19             our $VERSION = '1.12';
20            
21             #------------------------------------------------------------------------------
22            
23             =head1 SYNOPSIS
24            
25             use Parse::FSM::Error qw( error warning );
26            
27             error($message);
28             error($message, $file, $line_nr);
29            
30             warning($message);
31             warning($message, $file, $line_nr);
32            
33             =head1 DESCRIPTION
34            
35             This module formats an error or warning message and displays it on C,
36             exiting with die for C.
37            
38             =head1 EXPORTS
39            
40             None by default.
41            
42             =cut
43            
44             #------------------------------------------------------------------------------
45 2     2   8 use Exporter 'import';
  2         4  
  2         612  
46             our @EXPORT_OK = qw( error warning );
47             #------------------------------------------------------------------------------
48            
49             =head2 error
50            
51             Formats the error message, shows it on C and dies.
52             The file name and line number are optional.
53            
54             =cut
55            
56             #------------------------------------------------------------------------------
57             sub error {
58 27     27 1 8504 die _error_msg("Error", @_);
59             }
60             #------------------------------------------------------------------------------
61            
62             =head2 warning
63            
64             Formats the warning message and shows it on C.
65             The file name and line number are optional.
66            
67             =cut
68            
69             #------------------------------------------------------------------------------
70             sub warning {
71 23     23 1 7633 warn _error_msg("Warning", @_);
72             }
73            
74             #------------------------------------------------------------------------------
75             sub _error_msg {
76 50     50   82 my($type, $message, $file, $line_nr) = @_;
77            
78 50 100       238 $file = defined($file) ? "file '$file'" : undef;
79 50 100       97 $line_nr = $line_nr ? "line $line_nr" : undef;
80             my $pos = (defined($file) || defined($line_nr)) ?
81 50 100 100     194 "at ".join(", ", grep {defined} $file, $line_nr) :
  58         135  
82             undef;
83            
84 50 100       102 if (defined($message)) {
85 48         144 $message =~ s/\s+\z//; # in case message comes from die, has a "\n"
86 48 100       87 if ($message eq "") {
87 4         7 undef $message;
88             }
89             else {
90 44         78 $message = ": ".$message;
91             }
92             }
93            
94 50         157 return join(" ", grep {defined} $type, $pos, $message)."\n";
  150         635  
95             }
96            
97             #------------------------------------------------------------------------------
98            
99             =head1 AUTHOR, BUGS, FEEDBACK, LICENSE, COPYRIGHT
100            
101             See L
102            
103             =cut
104            
105             #------------------------------------------------------------------------------
106            
107             1;