File Coverage

blib/lib/App/Nopaste.pm
Criterion Covered Total %
statement 48 56 85.7
branch 13 26 50.0
condition 3 13 23.0
subroutine 10 12 83.3
pod 1 2 50.0
total 75 109 68.8


line stmt bran cond sub pod time code
1 4     4   615 use strict;
  4         8  
  4         103  
2 4     4   21 use warnings;
  4         8  
  4         158  
3             package App::Nopaste; # git description: 1.011-7-gc9be2f6
4             # ABSTRACT: Easy access to any pastebin
5             # KEYWORDS: application executable paste output pastebin debugging
6              
7             our $VERSION = '1.012';
8              
9 4     4   58 use 5.008003;
  4         12  
10 4     4   1655 use Module::Pluggable search_path => 'App::Nopaste::Service', sub_name => '_plugins';
  4         31189  
  4         23  
11 4     4   1970 use Class::Load 'load_class';
  4         26416  
  4         217  
12 4     4   1588 use namespace::clean 0.19;
  4         25252  
  4         20  
13              
14 4     4   1696 use parent 'Exporter';
  4         540  
  4         28  
15             our @EXPORT_OK = 'nopaste';
16              
17 1     1 0 108 sub plugins { goto \&_plugins };
18              
19             sub nopaste {
20             # process arguments
21             # allow "nopaste($text)"
22 2 50   2 1 8 unshift @_, 'text' if @_ == 1;
23              
24             # only look for $self if we have odd number of arguments
25 2         4 my $self;
26 2 50       22 $self = @_ % 2 ? shift : __PACKAGE__;
27              
28             # everything else
29 2         12 my %args = @_;
30              
31             $args{services} = defined($ENV{NOPASTE_SERVICES})
32             && [split ' ', $ENV{NOPASTE_SERVICES}]
33 2 50 0     8 if !defined($args{services});
34              
35             $args{nick} = $ENV{NOPASTE_NICK} || $ENV{USER}
36 2 50 0     7 if !defined($args{nick});
37              
38              
39 2         4 my $using_default = 0;
40 2 50 33     9 unless (ref($args{services}) eq 'ARRAY' && @{$args{services}}) {
  2         8  
41 0         0 $using_default = 1;
42 0         0 $args{services} = [ $self->plugins ];
43             }
44              
45 2 50       2 @{ $args{services} }
  2         7  
46             or Carp::croak "No App::Nopaste::Service module found";
47              
48             defined $args{text}
49 2 50       6 or Carp::croak "You must specify the text to nopaste";
50              
51             $args{error_handler} ||= sub {
52 0     0   0 my ($msg, $srv) = @_;
53 0         0 $msg =~ s/\n*$/\n/;
54 0         0 warn "$srv: $msg"
55 2   50     19 };
56              
57             # try to paste to each service in order
58 2         4 for my $service (@{ $args{services} }) {
  2         6  
59 2 50       14 $service = "App::Nopaste::Service::$service"
60             unless $service =~ /^App::Nopaste::Service/;
61              
62 4     4   1230 no warnings 'exiting';
  4         8  
  4         843  
63 2         4 my @ret = eval {
64              
65             local $SIG{__WARN__} = sub {
66 0     0   0 $args{warn_handler}->($_[0], $service);
67 2 50       5 } if $args{warn_handler};
68              
69 2         9 load_class($service);
70              
71 2 50       218 next unless $service->available(%args);
72 2 50 33     11 next if $using_default && $service->forbid_in_default;
73 2         15 $service->nopaste(%args);
74             };
75              
76 2 50       85 @ret = (0, $@) if $@;
77              
78             # success!
79 2 50       19 return $ret[1] if $ret[0];
80              
81             # failure!
82 0           $args{error_handler}->($ret[1], $service);
83             }
84              
85 0           Carp::croak "No available App::Nopaste::Service modules found";
86             }
87              
88             1;
89              
90             __END__