File Coverage

blib/lib/Script/Carp.pm
Criterion Covered Total %
statement 36 39 92.3
branch 8 12 66.6
condition 5 12 41.6
subroutine 8 8 100.0
pod n/a
total 57 71 80.2


line stmt bran cond sub pod time code
1             package Script::Carp;
2              
3 3     3   43401 use Carp ();
  3         8  
  3         66  
4 3     3   14 use strict;
  3         6  
  3         88  
5 3     3   15 use warnings;
  3         14  
  3         3484  
6              
7             our $IGNORE_EVAL = 0; # for test
8              
9             my $_die = sub {};
10              
11             *CORE::GLOBAL::die = sub {
12 1     1   224 my ($package, $filename, $line) = caller;
13              
14 1 50 33     13 if (defined $_[0] and $_[0] =~/^Died /) {
    50 33        
15 0         0 $_die->(@_)
16             } elsif (defined $_[0] and $_[0] =~/ at $filename line $line\./) {
17 0         0 $_die->('Died ', @_);
18             } else {
19 1         10 $_die->('Died ', @_, " at $filename line $line.\n")
20             }
21             };
22             $SIG{__DIE__} = sub { $_die->(@_) };
23              
24             our $FLGS =
25             {
26             -file => sub {
27             my ($args) = @_;
28             my $file = shift @$args;
29             Carp::croak("USAGE: use Script::Carp -file => 'file_name'") unless $file;
30             return sub {
31             my (@args) = @_;
32             my ($package, $filename, $line) = caller(1);
33             open my $out, ">", $file or die "cannot open file '$file'.";
34             print $out @args;
35             close $out;
36             };
37             },
38             -stop => sub {
39             return sub { print STDERR "Hit Enter to exit:"; <> };
40             },
41             -log => sub {
42             my ($args) = @_;
43             my $file = shift @$args;
44             Carp::croak("USAGE: use Script::Carp -log => 'file_name'") unless $file;
45             return sub {
46             my @args = @_;
47             open my $out, ">>", $file or die "cannot open file '$file'.";
48             print $out scalar localtime, "\n";
49             print $out @args;
50             close $out;
51             };
52             },
53             -clip => sub {
54             eval "require Clipboard; Clipboard->import()";
55             unless($@) {
56             my ($args) = @_;
57             return sub {
58             eval {
59             Clipboard->copy(join "", @_) if @_;
60             };
61             if ($@) {
62             warn $@;
63             }
64             }
65             } else {
66             my $msg = "You need Clipboard module.";
67             warn $msg;
68             return sub {};
69             }
70             },
71             -beep => sub {
72             local $@;
73             eval "require Audio::Beep";
74             unless ($@) {
75             my ($args) = @_;
76             $args->[0] ||= "c' d' e'";
77             my $beep = $args->[0] =~/^-/ ? "c' d' e'" : shift @$args;
78             return sub {
79             Audio::Beep->new->play($beep);
80             }
81             } else {
82             my $msg = "You need Audio::Beep module.";
83             $msg .= ' you may need "modprobe pcspkr" and/or "xset b on"' if $^O =~/linux/i;
84             warn $msg;
85             return sub {};
86             }
87             },
88             -ignore_eval => sub {
89             # only for test
90             $IGNORE_EVAL = 1;
91             return sub { };
92             },
93             };
94              
95             sub import {
96 3     3   31 my ($self, @opt) = @_;
97              
98 3         6 my @subs;
99 3         15 while (@opt) {
100 4         9 my $flg = shift @opt;
101 4 50       15 if (my $gen = $FLGS->{$flg}) {
102 4         10 push @subs, $gen->(\@opt);
103             }
104             }
105 3     3   20 no warnings;
  3         6  
  3         1205  
106             $_die = sub {
107 7     7   51 my ($package, $filename, $line) = caller(1);
108 7         37 my @args = @_;
109 7 50 66     54 if (! $IGNORE_EVAL and defined $^S and $^S == 1) {
      33        
110 0         0 CORE::die @args;
111             } else {
112 7 100       359 print STDERR @args, $IGNORE_EVAL ? '' : " at $filename line $line.\n";
113 7         57 _auto_flush();
114 7         17 for (@subs) {
115 12         21 _auto_flush();
116 12         34 $_->(@args);
117             }
118 7 100       187 exit 255 unless $IGNORE_EVAL;
119             }
120 3         67 };
121             }
122              
123             sub _auto_flush {
124 19     19   33 $| = 1;
125 19         43 my $fh = select;
126 19         33 select STDERR;
127 19         25 $| = 1;
128 19         53 select $fh;
129             }
130              
131             *setup = \&import;
132              
133             =head1 NAME
134              
135             Script::Carp - provide some ways to leave messages when script died
136              
137             =cut
138              
139             our $VERSION = '0.05';
140              
141              
142             =head1 SYNOPSIS
143              
144             use this with options.
145              
146             use Script::Carp -stop; # display error and wait STDIN
147             use Script::Carp -file => "error.txt"; # write message to error.txt
148             use Script::Carp -stop, -file => "error.txt"; # mixed the above
149             use Script::Carp -log => "error_log.txt"; # append message to error_log.txt
150             use Script::Carp -beep => "c d e f g"; # beep
151             use Script::Carp -clip; # message is copied to clipboard
152              
153             use class method with options
154              
155             Script::Carp->setup(-stop);
156             Script::Carp->setup(-file => "error.txt");
157             Script::Carp->setup(-log => "error_log.txt");
158             Script::Carp->setup(-stop, -file => "error.txt");
159             Script::Carp->setup(-beep => "c d e f g");
160             Script::Carp->setup(-clip);
161              
162             =head1 DESCRIPTION
163              
164             When you write script on MS Windows and run it and then it died,
165             prompt window is immediately clesed and you cannot see any messages.
166              
167             For such case, this module is usefule.
168              
169             When You use this module with some options or use setup method with some options,
170             you can check error message, easily.
171              
172             =head1 OPTIONS
173              
174             options can be used with use Script::Carp or class method setup method.
175              
176             =head2 -stop
177              
178             use Script::Carp -stop;
179              
180             or
181              
182             Script::Carp->setup(-stop);
183              
184             When script died, display messages and wait STDIN.
185              
186             =head2 -file => 'file_name'
187              
188             use Script::Carp -file => 'file_name';
189              
190             or
191              
192             Script::Carp->setup(-file => 'file_name');
193              
194              
195             When script died, messages are written to "file_name".
196              
197             =head2 -log => 'log_file_name'
198              
199             use Script::Carp -log => 'log_file_name';
200              
201             or
202              
203             Script::Carp->setup(-log => 'log_file_name');
204              
205             It is like file, but it will not clear file content.
206             When script died, messages are appended to "log_file_name".
207              
208             =head2 -beep
209              
210             use Script::Carp -beep;
211             use Script::Carp -beep => "c d e f g";
212              
213             or
214              
215             Script::Carp->setup(-beep);
216             Script::Carp->setup(-beep => "c d e f g");
217              
218             beep when script died. It requires Audio::Beep module.
219             If you use Linux, you may need to 'modprobe pcspkr", 'xset -b on'
220              
221             =head2 -clip
222              
223             use Script::Carp -clip;
224              
225             or
226              
227             Script::Carp->setup(-clip);
228              
229             message is copied to clipboard when script died.
230             It requires Clipboard module.
231              
232             =head1 METHOD
233              
234             =head2 setup
235              
236             see L and L
237              
238             =head1 IN eval BLOCK
239              
240             Script::Carp just die in eval block, error messages will be set to $@ as usual.
241              
242             use Script::Carp -stop;
243            
244             eval {
245             die "Script::Carp is ignored?"; # Yes, Sctip::Carp is ignored.
246             };
247             die $@ if $@; # Script::Carp work, here.
248              
249             =head1 AUTHOR
250              
251             Ktat, C<< >>
252              
253             =head1 BUGS
254              
255             Please report any bugs or feature requests to C, or through
256             the web interface at L. I will be notified, and then you'll
257             automatically be notified of progress on your bug as I make changes.
258              
259             =head1 SUPPORT
260              
261             You can find documentation for this module with the perldoc command.
262              
263             perldoc Script::Carp
264              
265              
266             You can also look for information at:
267              
268             =over 4
269              
270             =item * RT: CPAN's request tracker
271              
272             L
273              
274             =item * AnnoCPAN: Annotated CPAN documentation
275              
276             L
277              
278             =item * CPAN Ratings
279              
280             L
281              
282             =item * Search CPAN
283              
284             L
285              
286             =back
287              
288             =head1 ACKNOWLEDGEMENTS
289              
290             =head1 COPYRIGHT & LICENSE
291              
292             Copyright 2009 Ktat, all rights reserved.
293              
294             This program is free software; you can redistribute it and/or modify it
295             under the same terms as Perl itself.
296              
297              
298             =cut
299              
300             1; # End of Script::Carp