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 32     32   892 use strict;
  32         73  
  32         1282  
4 32     32   157 use warnings;
  32         54  
  32         2456  
5              
6             our $VERSION = '0.53';
7              
8 32     32   191 use Specio::OO;
  32         55  
  32         11803  
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 9406     9406   16770 return $attrs;
33             }
34             }
35              
36             sub new_from_caller {
37 991     991 1 1606 my $class = shift;
38 991         1480 my $depth = shift;
39              
40 991         1329 my %p;
41 991         7772 @p{qw( package filename line )} = ( caller($depth) )[ 0, 1, 2 ];
42              
43 991         4493 my $sub = ( caller( $depth + 1 ) )[3];
44 991 100       3118 $p{subroutine} = $sub if defined $sub;
45              
46 991         3816 return $class->new(%p);
47             }
48              
49             sub description {
50 968     968 1 3849 my $self = shift;
51              
52 968         2706 my $package = $self->package;
53 968         4573 my $filename = $self->filename;
54 968         4222 my $line = $self->line;
55              
56 968         4113 my $desc = "declared in package $package ($filename) at line $line";
57 968 100       2228 if ( $self->has_subroutine ) {
58 895         4130 $desc .= ' in sub named ' . $self->subroutine;
59             }
60              
61 968         6236 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.53
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             =head1 SOURCE
128              
129             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
130              
131             =head1 AUTHOR
132              
133             Dave Rolsky <autarch@urth.org>
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             This software is Copyright (c) 2012 - 2025 by Dave Rolsky.
138              
139             This is free software, licensed under:
140              
141             The Artistic License 2.0 (GPL Compatible)
142              
143             The full text of the license can be found in the
144             F<LICENSE> file included with this distribution.
145              
146             =cut