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   766 use strict;
  4         13  
  4         192  
2 4     4   46 use warnings;
  4         9  
  4         281  
3             package App::Nopaste; # git description: 1.010-5-ge64d42a
4             # ABSTRACT: Easy access to any pastebin
5             # KEYWORDS: application executable paste output pastebin debugging
6              
7             our $VERSION = '1.011';
8              
9 4     4   88 use 5.008003;
  4         19  
10 4     4   1460 use Module::Pluggable search_path => 'App::Nopaste::Service', sub_name => '_plugins';
  4         34857  
  4         38  
11 4     4   1918 use Class::Load 'load_class';
  4         25924  
  4         298  
12 4     4   1334 use namespace::clean 0.19;
  4         27158  
  4         39  
13              
14 4     4   1580 use parent 'Exporter';
  4         457  
  4         28  
15             our @EXPORT_OK = 'nopaste';
16              
17 1     1 0 170 sub plugins { goto \&_plugins };
18              
19             sub nopaste {
20             # process arguments
21             # allow "nopaste($text)"
22 2 50   2 1 11 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       10 $self = @_ % 2 ? shift : __PACKAGE__;
27              
28             # everything else
29 2         23 my %args = @_;
30              
31             $args{services} = defined($ENV{NOPASTE_SERVICES})
32             && [split ' ', $ENV{NOPASTE_SERVICES}]
33 2 50 0     10 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         7 my $using_default = 0;
40 2 50 33     12 unless (ref($args{services}) eq 'ARRAY' && @{$args{services}}) {
  2         11  
41 0         0 $using_default = 1;
42 0         0 $args{services} = [ $self->plugins ];
43             }
44              
45 2 50       6 @{ $args{services} }
  2         8  
46             or Carp::croak "No App::Nopaste::Service module found";
47              
48             defined $args{text}
49 2 50       8 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     26 };
56              
57             # try to paste to each service in order
58 2         4 for my $service (@{ $args{services} }) {
  2         8  
59 2 50       14 $service = "App::Nopaste::Service::$service"
60             unless $service =~ /^App::Nopaste::Service/;
61              
62 4     4   1576 no warnings 'exiting';
  4         10  
  4         935  
63 2         6 my @ret = eval {
64              
65             local $SIG{__WARN__} = sub {
66 0     0   0 $args{warn_handler}->($_[0], $service);
67 2 50       8 } if $args{warn_handler};
68              
69 2         12 load_class($service);
70              
71 2 50       271 next unless $service->available(%args);
72 2 50 33     16 next if $using_default && $service->forbid_in_default;
73 2         24 $service->nopaste(%args);
74             };
75              
76 2 50       140 @ret = (0, $@) if $@;
77              
78             # success!
79 2 50       31 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__