File Coverage

blib/lib/Contextual/Diag.pm
Criterion Covered Total %
statement 50 50 100.0
branch 16 16 100.0
condition 4 4 100.0
subroutine 18 18 100.0
pod 1 1 100.0
total 89 89 100.0


line stmt bran cond sub pod time code
1             package Contextual::Diag;
2 2     2   250570 use 5.010;
  2         18  
3 2     2   12 use strict;
  2         4  
  2         54  
4 2     2   21 use warnings;
  2         5  
  2         109  
5              
6             our $VERSION = "0.04";
7              
8 2     2   14 use Exporter;
  2         3  
  2         152  
9             our @ISA = qw/Exporter/;
10             our @EXPORT = qw/contextual_diag/;
11              
12 2     2   22 use Carp ();
  2         5  
  2         46  
13 2     2   899 use Contextual::Diag::Value;
  2         5  
  2         622  
14              
15             sub contextual_diag {
16              
17 42 100   42 1 46289 if (wantarray) {
    100          
18 11         34 _diag('wanted LIST context');
19 11         93 return @_;
20             }
21             elsif(!defined wantarray) {
22 2         10 _diag('wanted VOID context');
23 2         17 return;
24             }
25             else {
26 29         87 _diag('wanted SCALAR context');
27              
28             return Contextual::Diag::Value->new($_[0],
29 1     1   4 BOOL => sub { _diag('evaluated as BOOL in SCALAR context'); return $_[0] },
  1         9  
30 2   100 2   7 NUM => sub { _diag('evaluated as NUM in SCALAR context'); return $_[0] || 0 },
  2         39  
31 3   100 3   8 STR => sub { _diag('evaluated as STR in SCALAR context'); return $_[0] || "" },
  3         43  
32 2 100   2   6 SCALARREF => sub { _diag('scalar ref is evaluated as SCALARREF'); return defined $_[0] ? $_[0] : \"" },
  2         44  
33 3 100   3   8 ARRAYREF => sub { _diag('scalar ref is evaluated as ARRAYREF'); return defined $_[0] ? $_[0] : [] },
  3         52  
34 3 100   3   8 HASHREF => sub { _diag('scalar ref is evaluated as HASHREF'); return defined $_[0] ? $_[0] : {} },
  3         54  
35 2 100   2   6 CODEREF => sub { _diag('scalar ref is evaluated as CODEREF'); return defined $_[0] ? $_[0] : sub { } },
  2         43  
36 2 100   2   18 GLOBREF => sub { _diag('scalar ref is evaluated as GLOBREF'); return defined $_[0] ? $_[0] : do { no strict qw/refs/; my $package = __PACKAGE__; \*{$package} } },
  2     2   4  
  2         342  
  2         7  
  2         41  
  1         3  
  1         2  
  1         8  
37 7 100   7   17 OBJREF => sub { _diag('scalar ref is evaluated as OBJREF'); return defined $_[0] ? $_[0] : bless {}, __PACKAGE__ },
  7         206  
38 29         910 );
39             }
40             }
41              
42             sub _diag {
43 67     67   138 local $Carp::CarpLevel = 2;
44 67         7262 goto &Carp::carp;
45             }
46              
47             1;
48             __END__