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   212166 use 5.010;
  2         12  
3 2     2   9 use strict;
  2         3  
  2         37  
4 2     2   16 use warnings;
  2         5  
  2         101  
5              
6             our $VERSION = "0.03";
7              
8 2     2   12 use Exporter;
  2         2  
  2         139  
9             our @ISA = qw/Exporter/;
10             our @EXPORT = qw/contextual_diag/;
11              
12 2     2   12 use Carp ();
  2         11  
  2         46  
13 2     2   816 use Contextual::Diag::Value;
  2         5  
  2         498  
14              
15             sub contextual_diag {
16              
17 39 100   39 1 34734 if (wantarray) {
    100          
18 10         24 _diag('wanted LIST context');
19 10         70 return @_;
20             }
21             elsif(!defined wantarray) {
22 1         5 _diag('wanted VOID context');
23 1         8 return;
24             }
25             else {
26 28         118 _diag('wanted SCALAR context');
27              
28             return Contextual::Diag::Value->new($_[0],
29 1     1   3 BOOL => sub { _diag('evaluated as BOOL in SCALAR context'); return $_[0] },
  1         7  
30 2   100 2   7 NUM => sub { _diag('evaluated as NUM in SCALAR context'); return $_[0] || 0 },
  2         26  
31 3   100 3   5 STR => sub { _diag('evaluated as STR in SCALAR context'); return $_[0] || "" },
  3         36  
32 2 100   2   4 SCALARREF => sub { _diag('scalar ref is evaluated as SCALARREF'); return defined $_[0] ? $_[0] : \"" },
  2         34  
33 3 100   3   7 ARRAYREF => sub { _diag('scalar ref is evaluated as ARRAYREF'); return defined $_[0] ? $_[0] : [] },
  3         40  
34 3 100   3   9 HASHREF => sub { _diag('scalar ref is evaluated as HASHREF'); return defined $_[0] ? $_[0] : {} },
  3         42  
35 2 100   2   5 CODEREF => sub { _diag('scalar ref is evaluated as CODEREF'); return defined $_[0] ? $_[0] : sub { } },
  2         34  
36 2 100   2   13 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         298  
  2         6  
  2         34  
  1         16  
  1         2  
  1         7  
37 7 100   7   45 OBJREF => sub { _diag('scalar ref is evaluated as OBJREF'); return defined $_[0] ? $_[0] : bless {}, __PACKAGE__ },
  7         168  
38 28         617 );
39             }
40             }
41              
42             sub _diag {
43 64     64   99 local $Carp::CarpLevel = 2;
44 64         5527 goto &Carp::carp;
45             }
46              
47             1;
48             __END__