File Coverage

blib/lib/Specio/DeclaredAt.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 37 37 100.0


line stmt bran cond sub pod time code
1             package Specio::DeclaredAt;
2              
3 29     29   637 use strict;
  29         62  
  29         923  
4 29     29   152 use warnings;
  29         61  
  29         1196  
5              
6             our $VERSION = '0.47';
7              
8 29     29   166 use Specio::OO;
  29         57  
  29         9251  
9              
10             {
11             my $attrs = {
12             package => {
13             isa => 'Str',
14             required => 1,
15             },
16             filename => {
17             isa => 'Str',
18             required => 1,
19             },
20             line => {
21             isa => 'Int',
22             required => 1,
23             },
24             subroutine => {
25             isa => 'Str',
26             predicate => 'has_subroutine',
27             },
28             };
29              
30             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
31             sub _attrs {
32 9218     9218   14886 return $attrs;
33             }
34             }
35              
36             sub new_from_caller {
37 928     928 1 1580 my $class = shift;
38 928         1309 my $depth = shift;
39              
40 928         1312 my %p;
41 928         7908 @p{qw( package filename line )} = ( caller($depth) )[ 0, 1, 2 ];
42              
43 928         4528 my $sub = ( caller( $depth + 1 ) )[3];
44 928 100       2974 $p{subroutine} = $sub if defined $sub;
45              
46 928         3428 return $class->new(%p);
47             }
48              
49             sub description {
50 905     905 1 3549 my $self = shift;
51              
52 905         2190 my $package = $self->package;
53 905         3612 my $filename = $self->filename;
54 905         3314 my $line = $self->line;
55              
56 905         3546 my $desc = "declared in package $package ($filename) at line $line";
57 905 100       2158 if ( $self->has_subroutine ) {
58 838         3822 $desc .= ' in sub named ' . $self->subroutine;
59             }
60              
61 905         5654 return $desc;
62             }
63              
64             __PACKAGE__->_ooify;
65              
66             1;
67              
68             # ABSTRACT: A class to represent where a type or coercion was declared
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Specio::DeclaredAt - A class to represent where a type or coercion was declared
79              
80             =head1 VERSION
81              
82             version 0.47
83              
84             =head1 SYNOPSIS
85              
86             my $declared = Specio::DeclaredAt->new_from_caller(1);
87              
88             print $declared->description;
89              
90             =head1 DESCRIPTION
91              
92             This class provides a thin wrapper around some of the return values from Perl's
93             C<caller> built-in. It's used internally to identify where types and coercions
94             are being declared, which is useful when generating error messages.
95              
96             =head1 API
97              
98             This class provides the following methods.
99              
100             =head2 Specio::DeclaredAt->new_from_caller($depth)
101              
102             Given a call stack depth, this method returns a new C<Specio::DeclaredAt>
103             object.
104              
105             =head2 $declared_at->package, $declared_at->filename, $declared_at->line
106              
107             Returns the call stack information recorded when the object was created. These
108             values are always populated.
109              
110             =head2 $declared_at->subroutine
111              
112             Returns the subroutine from the call stack. This may be an C<udnef>
113              
114             =head2 $declared_at->has_subroutine
115              
116             Returns true if there is a subroutine name associated with this object.
117              
118             =head2 $declared_at->description
119              
120             Puts all the information together into a single string like "declared in
121             package Foo::Bar (.../Foo/Bar.pm) at line 42 in sub named blah".
122              
123             =head1 SUPPORT
124              
125             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
126              
127             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
128              
129             =head1 SOURCE
130              
131             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
132              
133             =head1 AUTHOR
134              
135             Dave Rolsky <autarch@urth.org>
136              
137             =head1 COPYRIGHT AND LICENSE
138              
139             This software is Copyright (c) 2012 - 2021 by Dave Rolsky.
140              
141             This is free software, licensed under:
142              
143             The Artistic License 2.0 (GPL Compatible)
144              
145             The full text of the license can be found in the
146             F<LICENSE> file included with this distribution.
147              
148             =cut