| 1 | package autodie::hints; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use constant PERL58 => ( $] < 5.009 ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | our $VERSION = '2.37'; # VERSION: Generated by DZP::OurPkg:Version | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | # ABSTRACT: Provide hints about user subroutines to autodie | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | autodie::hints - Provide hints about user subroutines to autodie | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | package Your::Module; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | our %DOES = ( 'autodie::hints::provider' => 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | sub AUTODIE_HINTS { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 24 | foo => { scalar => HINTS, list => SOME_HINTS }, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | bar => { scalar => HINTS, list => MORE_HINTS }, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # Later, in your main program... | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | use Your::Module qw(foo bar); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | use autodie qw(:default foo bar); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | foo(); # succeeds or dies based on scalar hints | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # Alternatively, hints can be set on subroutines we've | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # imported. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | use autodie::hints; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | use Some::Module qw(think_positive); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 44 | \&think_positive, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | fail => sub { $_[0] <= 0 } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | use autodie qw(think_positive); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | think_positive(...); # Returns positive or dies. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =head2 Introduction | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | The L<autodie> pragma is very smart when it comes to working with | ||||||||||||||||||||||||||||||||||||||||||
| 60 | Perl's built-in functions. The behaviour for these functions are | ||||||||||||||||||||||||||||||||||||||||||
| 61 | fixed, and C<autodie> knows exactly how they try to signal failure. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | But what about user-defined subroutines from modules? If you use | ||||||||||||||||||||||||||||||||||||||||||
| 64 | C<autodie> on a user-defined subroutine then it assumes the following | ||||||||||||||||||||||||||||||||||||||||||
| 65 | behaviour to demonstrate failure: | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | A false value, in scalar context | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | An empty list, in list context | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | A list containing a single undef, in list context | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | All other return values (including the list of the single zero, and the | ||||||||||||||||||||||||||||||||||||||||||
| 84 | list containing a single empty string) are considered successful. However, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | real-world code isn't always that easy. Perhaps the code you're working | ||||||||||||||||||||||||||||||||||||||||||
| 86 | with returns a string containing the word "FAIL" upon failure, or a | ||||||||||||||||||||||||||||||||||||||||||
| 87 | two element list containing C<(undef, "human error message")>. To make | ||||||||||||||||||||||||||||||||||||||||||
| 88 | autodie work with these sorts of subroutines, we have | ||||||||||||||||||||||||||||||||||||||||||
| 89 | the I<hinting interface>. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | The hinting interface allows I<hints> to be provided to C<autodie> | ||||||||||||||||||||||||||||||||||||||||||
| 92 | on how it should detect failure from user-defined subroutines. While | ||||||||||||||||||||||||||||||||||||||||||
| 93 | these I<can> be provided by the end-user of C<autodie>, they are ideally | ||||||||||||||||||||||||||||||||||||||||||
| 94 | written into the module itself, or into a helper module or sub-class | ||||||||||||||||||||||||||||||||||||||||||
| 95 | of C<autodie> itself. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =head2 What are hints? | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | A I<hint> is a subroutine or value that is checked against the | ||||||||||||||||||||||||||||||||||||||||||
| 100 | return value of an autodying subroutine. If the match returns true, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | C<autodie> considers the subroutine to have failed. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | If the hint provided is a subroutine, then C<autodie> will pass | ||||||||||||||||||||||||||||||||||||||||||
| 104 | the complete return value to that subroutine. If the hint is a regexp object, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | then C<autodie> will match it against the return value. If the hint is undef, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | the return value must be undef. On Perl versions 5.10 and newer, any other | ||||||||||||||||||||||||||||||||||||||||||
| 107 | value can be provided and it will be smart matched against the value provided. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | However, smart matched values like this are deprecated. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | Hints can be provided for both scalar and list contexts. Note | ||||||||||||||||||||||||||||||||||||||||||
| 111 | that an autodying subroutine will never see a void context, as | ||||||||||||||||||||||||||||||||||||||||||
| 112 | C<autodie> always needs to capture the return value for examination. | ||||||||||||||||||||||||||||||||||||||||||
| 113 | Autodying subroutines called in void context act as if they're called | ||||||||||||||||||||||||||||||||||||||||||
| 114 | in a scalar context, but their return value is discarded after it | ||||||||||||||||||||||||||||||||||||||||||
| 115 | has been checked. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =head2 Example hints | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | Hints may consist of subroutine references, objects overloading | ||||||||||||||||||||||||||||||||||||||||||
| 120 | smart-match, regular expressions, and depending on Perl version possibly | ||||||||||||||||||||||||||||||||||||||||||
| 121 | other things. You can specify different hints for how | ||||||||||||||||||||||||||||||||||||||||||
| 122 | failure should be identified in scalar and list contexts. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | These examples apply for use in the C<AUTODIE_HINTS> subroutine and when | ||||||||||||||||||||||||||||||||||||||||||
| 125 | calling C<< autodie::hints->set_hints_for() >>. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | The most common context-specific hints are: | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | # Scalar failures always return undef: | ||||||||||||||||||||||||||||||||||||||||||
| 130 | { scalar => sub { !defined($_[0]) } } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # Scalar failures return any false value [default expectation]: | ||||||||||||||||||||||||||||||||||||||||||
| 133 | { scalar => sub { ! $_[0] } } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # Scalar failures always return zero explicitly: | ||||||||||||||||||||||||||||||||||||||||||
| 136 | { scalar => sub { defined($_[0]) && $_[0] eq '0' } } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # List failures always return an empty list: | ||||||||||||||||||||||||||||||||||||||||||
| 139 | { list => sub { !@_ } } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # List failures return () or (undef) [default expectation]: | ||||||||||||||||||||||||||||||||||||||||||
| 142 | { list => sub { ! @_ || @_ == 1 && !defined $_[0] } } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | # List failures return () or a single false value: | ||||||||||||||||||||||||||||||||||||||||||
| 145 | { list => sub { ! @_ || @_ == 1 && !$_[0] } } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # List failures return (undef, "some string") | ||||||||||||||||||||||||||||||||||||||||||
| 148 | { list => sub { @_ == 2 && !defined $_[0] } } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # Unsuccessful foo() returns 'FAIL' or '_FAIL' in scalar context, | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # returns (-1) in list context... | ||||||||||||||||||||||||||||||||||||||||||
| 152 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 153 | \&foo, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | scalar => qr/^ _? FAIL $/xms, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | list => sub { @_ == 1 && $_[0] eq -1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | # Unsuccessful foo() returns 0 in all contexts... | ||||||||||||||||||||||||||||||||||||||||||
| 161 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 162 | \&foo, | ||||||||||||||||||||||||||||||||||||||||||
| 163 | { | ||||||||||||||||||||||||||||||||||||||||||
| 164 | scalar => sub { defined($_[0]) && $_[0] == 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | list => sub { @_ == 1 && defined($_[0]) && $_[0] == 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | This "in all contexts" construction is very common, and can be | ||||||||||||||||||||||||||||||||||||||||||
| 170 | abbreviated, using the 'fail' key. This sets both the C<scalar> | ||||||||||||||||||||||||||||||||||||||||||
| 171 | and C<list> hints to the same value: | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # Unsuccessful foo() returns 0 in all contexts... | ||||||||||||||||||||||||||||||||||||||||||
| 174 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 175 | \&foo, | ||||||||||||||||||||||||||||||||||||||||||
| 176 | { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | fail => sub { @_ == 1 and defined $_[0] and $_[0] == 0 } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | # Unsuccessful think_positive() returns negative number on failure... | ||||||||||||||||||||||||||||||||||||||||||
| 182 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 183 | \&think_positive, | ||||||||||||||||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||||||||||||||||
| 185 | fail => sub { $_[0] < 0 } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | # Unsuccessful my_system() returns non-zero on failure... | ||||||||||||||||||||||||||||||||||||||||||
| 190 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 191 | \&my_system, | ||||||||||||||||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||||||||||||||||
| 193 | fail => sub { $_[0] != 0 } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | =head1 Manually setting hints from within your program | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | If you are using a module which returns something special on failure, then | ||||||||||||||||||||||||||||||||||||||||||
| 200 | you can manually create hints for each of the desired subroutines. Once | ||||||||||||||||||||||||||||||||||||||||||
| 201 | the hints are specified, they are available for all files and modules loaded | ||||||||||||||||||||||||||||||||||||||||||
| 202 | thereafter, thus you can move this work into a module and it will still | ||||||||||||||||||||||||||||||||||||||||||
| 203 | work. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | use Some::Module qw(foo bar); | ||||||||||||||||||||||||||||||||||||||||||
| 206 | use autodie::hints; | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 209 | \&foo, | ||||||||||||||||||||||||||||||||||||||||||
| 210 | { | ||||||||||||||||||||||||||||||||||||||||||
| 211 | scalar => SCALAR_HINT, | ||||||||||||||||||||||||||||||||||||||||||
| 212 | list => LIST_HINT, | ||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 215 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 216 | \&bar, | ||||||||||||||||||||||||||||||||||||||||||
| 217 | { fail => SOME_HINT, } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | It is possible to pass either a subroutine reference (recommended) or a fully | ||||||||||||||||||||||||||||||||||||||||||
| 221 | qualified subroutine name as the first argument. This means you can set hints | ||||||||||||||||||||||||||||||||||||||||||
| 222 | on modules that I<might> get loaded: | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | use autodie::hints; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | autodie::hints->set_hints_for( | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 'Some::Module:bar', { fail => SCALAR_HINT, } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | This technique is most useful when you have a project that uses a | ||||||||||||||||||||||||||||||||||||||||||
| 230 | lot of third-party modules. You can define all your possible hints | ||||||||||||||||||||||||||||||||||||||||||
| 231 | in one-place. This can even be in a sub-class of autodie. For | ||||||||||||||||||||||||||||||||||||||||||
| 232 | example: | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | package my::autodie; | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | use parent qw(autodie); | ||||||||||||||||||||||||||||||||||||||||||
| 237 | use autodie::hints; | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | autodie::hints->set_hints_for(...); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | You can now C<use my::autodie>, which will work just like the standard | ||||||||||||||||||||||||||||||||||||||||||
| 244 | C<autodie>, but is now aware of any hints that you've set. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | =head1 Adding hints to your module | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | C<autodie> provides a passive interface to allow you to declare hints for | ||||||||||||||||||||||||||||||||||||||||||
| 249 | your module. These hints will be found and used by C<autodie> if it | ||||||||||||||||||||||||||||||||||||||||||
| 250 | is loaded, but otherwise have no effect (or dependencies) without autodie. | ||||||||||||||||||||||||||||||||||||||||||
| 251 | To set these, your module needs to declare that it I<does> the | ||||||||||||||||||||||||||||||||||||||||||
| 252 | C<autodie::hints::provider> role. This can be done by writing your | ||||||||||||||||||||||||||||||||||||||||||
| 253 | own C<DOES> method, using a system such as C<Class::DOES> to handle | ||||||||||||||||||||||||||||||||||||||||||
| 254 | the heavy-lifting for you, or declaring a C<%DOES> package variable | ||||||||||||||||||||||||||||||||||||||||||
| 255 | with a C<autodie::hints::provider> key and a corresponding true value. | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | Note that checking for a C<%DOES> hash is an C<autodie>-only | ||||||||||||||||||||||||||||||||||||||||||
| 258 | short-cut. Other modules do not use this mechanism for checking | ||||||||||||||||||||||||||||||||||||||||||
| 259 | roles, although you can use the C<Class::DOES> module from the | ||||||||||||||||||||||||||||||||||||||||||
| 260 | CPAN to allow it. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | In addition, you must define a C<AUTODIE_HINTS> subroutine that returns | ||||||||||||||||||||||||||||||||||||||||||
| 263 | a hash-reference containing the hints for your subroutines: | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | package Your::Module; | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | # We can use the Class::DOES from the CPAN to declare adherence | ||||||||||||||||||||||||||||||||||||||||||
| 268 | # to a role. | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | use Class::DOES 'autodie::hints::provider' => 1; | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | # Alternatively, we can declare the role in %DOES. Note that | ||||||||||||||||||||||||||||||||||||||||||
| 273 | # this is an autodie specific optimisation, although Class::DOES | ||||||||||||||||||||||||||||||||||||||||||
| 274 | # can be used to promote this to a true role declaration. | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | our %DOES = ( 'autodie::hints::provider' => 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | # Finally, we must define the hints themselves. | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | sub AUTODIE_HINTS { | ||||||||||||||||||||||||||||||||||||||||||
| 281 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 282 | foo => { scalar => HINTS, list => SOME_HINTS }, | ||||||||||||||||||||||||||||||||||||||||||
| 283 | bar => { scalar => HINTS, list => MORE_HINTS }, | ||||||||||||||||||||||||||||||||||||||||||
| 284 | baz => { fail => HINTS }, | ||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | This allows your code to set hints without relying on C<autodie> and | ||||||||||||||||||||||||||||||||||||||||||
| 289 | C<autodie::hints> being loaded, or even installed. In this way your | ||||||||||||||||||||||||||||||||||||||||||
| 290 | code can do the right thing when C<autodie> is installed, but does not | ||||||||||||||||||||||||||||||||||||||||||
| 291 | need to depend upon it to function. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | =head1 Insisting on hints | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | When a user-defined subroutine is wrapped by C<autodie>, it will | ||||||||||||||||||||||||||||||||||||||||||
| 296 | use hints if they are available, and otherwise reverts to the | ||||||||||||||||||||||||||||||||||||||||||
| 297 | I<default behaviour> described in the introduction of this document. | ||||||||||||||||||||||||||||||||||||||||||
| 298 | This can be problematic if we expect a hint to exist, but (for | ||||||||||||||||||||||||||||||||||||||||||
| 299 | whatever reason) it has not been loaded. | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | We can ask autodie to I<insist> that a hint be used by prefixing | ||||||||||||||||||||||||||||||||||||||||||
| 302 | an exclamation mark to the start of the subroutine name. A lone | ||||||||||||||||||||||||||||||||||||||||||
| 303 | exclamation mark indicates that I<all> subroutines after it must | ||||||||||||||||||||||||||||||||||||||||||
| 304 | have hints declared. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | # foo() and bar() must have their hints defined | ||||||||||||||||||||||||||||||||||||||||||
| 307 | use autodie qw( !foo !bar baz ); | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | # Everything must have hints (recommended). | ||||||||||||||||||||||||||||||||||||||||||
| 310 | use autodie qw( ! foo bar baz ); | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | # bar() and baz() must have their hints defined | ||||||||||||||||||||||||||||||||||||||||||
| 313 | use autodie qw( foo ! bar baz ); | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | # Enable autodie for all of Perl's supported built-ins, | ||||||||||||||||||||||||||||||||||||||||||
| 316 | # as well as for foo(), bar() and baz(). Everything must | ||||||||||||||||||||||||||||||||||||||||||
| 317 | # have hints. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | use autodie qw( ! :all foo bar baz ); | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | If hints are not available for the specified subroutines, this will cause a | ||||||||||||||||||||||||||||||||||||||||||
| 321 | compile-time error. Insisting on hints for Perl's built-in functions | ||||||||||||||||||||||||||||||||||||||||||
| 322 | (eg, C<open> and C<close>) is always successful. | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | Insisting on hints is I<strongly> recommended. | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | # TODO: implement regular expression hints | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | use constant UNDEF_ONLY => sub { not defined $_[0] }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 12 | ▶ | use constant EMPTY_OR_UNDEF => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | ! @_ or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
!@_ || @_ == 1 && !defined($_[0])
@_ == 1 and not defined $_[0]
MC/DC0%0/3
!@_ || @_ == 1 && !defined($_[0])
!@_
@_ == 1
not defined $_[0]
Truth table0%0/4
not(A) || B and not(C)
A= !@_B= @_ == 1C= not defined $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | @_==1 && !defined $_[0] | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | use constant EMPTY_ONLY => sub { @_ == 0 }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 12 | ▶ | use constant EMPTY_OR_FALSE => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | ! @_ or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
!@_ || @_ == 1 && !$_[0]
@_ == 1 and not $_[0]
MC/DC0%0/3
!@_ || @_ == 1 && !$_[0]
!@_
@_ == 1
not $_[0]
Truth table0%0/4
not(A) || B and not(C)
A= !@_B= @_ == 1C= not $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | @_==1 && !$_[0] | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 12 | ▶ | use constant SINGLE_TRUE => sub { @_ == 1 and not $_[0] }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 12 | ▶ | use constant DEFAULT_HINTS => { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | scalar => UNDEF_ONLY, | ||||||||||||||||||||||||||||||||||||||||||
| 346 | list => EMPTY_OR_UNDEF, | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 12 | ▶ | use constant HINTS_PROVIDER => 'autodie::hints::provider'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | 12 | our $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | # Only ( undef ) is a strange but possible situation for very | ||||||||||||||||||||||||||||||||||||||||||
| 355 | # badly written code. It's not supported yet. | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | 12 | my %Hints = ( | |||||||||||||||||||||||||||||||||||||||||
| 358 | 'File::Copy::copy' => { scalar => SINGLE_TRUE, list => SINGLE_TRUE }, | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 'File::Copy::move' => { scalar => SINGLE_TRUE, list => SINGLE_TRUE }, | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 'File::Copy::cp' => { scalar => SINGLE_TRUE, list => SINGLE_TRUE }, | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 'File::Copy::mv' => { scalar => SINGLE_TRUE, list => SINGLE_TRUE }, | ||||||||||||||||||||||||||||||||||||||||||
| 362 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | # Start by using Sub::Identify if it exists on this system. | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | 12 | eval { require Sub::Identify; Sub::Identify->import('get_code_info'); }; | |||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | # If it doesn't exist, we'll define our own. This code is directly | ||||||||||||||||||||||||||||||||||||||||||
| 369 | # taken from Rafael Garcia's Sub::Identify 0.04, used under the same | ||||||||||||||||||||||||||||||||||||||||||
| 370 | # license as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 12 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | require B; | |||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 12 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | *get_code_info = sub ($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | my ($coderef) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | ref $coderef or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | my $cv = B::svref_2object($coderef); | |||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | ▶ | $cv->isa('B::CV') or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | # bail out if GV is undefined | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | ▶ | $cv->GV->isa('B::SPECIAL') and return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | return ($cv->GV->STASH->NAME, $cv->GV->NAME); | |||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | 143 | ▶ | sub sub_fullname { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sub_fullname: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | 143 | return join( '::', get_code_info( $_[1] ) ); | |||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 12 | my %Hints_loaded = (); | |||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 30 | ▶ | sub load_hints { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_hints: called CC=9 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 30 | my ($class, $sub) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | 30 | my ($package) = ( $sub =~ /(.*)::/ ); | |||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | 30 | ▶ | if (not defined $package) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | Carp::croak( | |||||||||||||||||||||||||||||||||||||||||
| 405 | "Internal error in autodie::hints::load_hints - no package found. | ||||||||||||||||||||||||||||||||||||||||||
| 406 | "); | ||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | # Do nothing if we've already tried to load hints for | ||||||||||||||||||||||||||||||||||||||||||
| 410 | # this package. | ||||||||||||||||||||||||||||||||||||||||||
| 411 | 30 | ▶ | return if $Hints_loaded{$package}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | 9 | my $hints_available = 0; | |||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | { | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 12 | ▶ | no strict 'refs'; ## no critic | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | 9 | ▶ | if ($package->can('DOES') and $package->DOES(HINTS_PROVIDER) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$package->can("DOES") and $package->DOES("autodie::hints::provider")
MC/DC50%1/2
$package->can("DOES") and $package->DOES("autodie::hints::provider")
$package->can("DOES")
$package->DOES("autodie::hints::provider")
Truth table66%2/3
A and B
A= $package->can("DOES")B= $package->DOES("autodie::hints::provider")
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 3 | $hints_available = 1; | |||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | elsif ( PERL58 and $package->isa(HINTS_PROVIDER) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 422 | $hints_available = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 6 | elsif ( ${"${package}::DOES"}{HINTS_PROVIDER.""} ) { | |||||||||||||||||||||||||||||||||||||||||
| 425 | 2 | $hints_available = 1; | |||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 9 | ▶ | return if not $hints_available; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | 5 | my %package_hints = %{ $package->AUTODIE_HINTS }; | |||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | 5 | foreach my $sub (keys %package_hints) { | |||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | 15 | my $hint = $package_hints{$sub}; | |||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | # Ensure we have a package name. | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 15 | ▶ | $sub = "${package}::$sub" if $sub !~ /::/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | # TODO - Currently we don't check for conflicts, should we? | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 15 | $Hints{$sub} = $hint; | |||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 15 | $class->normalise_hints(\%Hints, $sub); | |||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | 24 | ▶ | sub normalise_hints { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normalise_hints: called CC=6 SCAR=21.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 24 | my ($class, $hints, $sub) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 24 | ▶ | if ( exists $hints->{$sub}->{fail} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | 6 | ▶ | if ( exists $hints->{$sub}->{scalar} or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $hints->{$sub}{"scalar"} or exists $hints->{$sub}{"list"}
MC/DC0%0/2
exists $hints->{$sub}{"scalar"} or exists $hints->{$sub}{"list"}
exists $hints->{$sub}{"scalar"}
exists $hints->{$sub}{"list"}
Truth table33%1/3
A or B
A= exists $hints->{$sub}{"scalar"}B= exists $hints->{$sub}{"list"}
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | exists $hints->{$sub}->{list} | ||||||||||||||||||||||||||||||||||||||||||
| 457 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 458 | # TODO: Turn into a proper diagnostic. | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | local $Carp::CarpLevel = 1; | |||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | Carp::croak("fail hints cannot be provided with either scalar or list hints for $sub"); | |||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | # Set our scalar and list hints. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | $hints->{$sub}->{scalar} = | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 6 | $hints->{$sub}->{list} = delete $hints->{$sub}->{fail}; | |||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | # Check to make sure all our hints exist. | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | 18 | foreach my $hint (qw(scalar list)) { | |||||||||||||||||||||||||||||||||||||||||
| 476 | 36 | ▶ | if ( not exists $hints->{$sub}->{$hint} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | # TODO: Turn into a proper diagnostic. | ||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | local $Carp::CarpLevel = 1; | |||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | Carp::croak("$hint hint missing for $sub"); | |||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | 18 | return; | |||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 89 | ▶ | sub get_hints_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_hints_for: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | 89 | my ($class, $sub) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | 89 | my $subname = $class->sub_fullname( $sub ); | |||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | # If we have hints loaded for a sub, then return them. | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | 89 | ▶ | if ( exists $Hints{ $subname } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | 59 | return $Hints{ $subname }; | |||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | # If not, we try to load them... | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | 30 | $class->load_hints( $subname ); | |||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | # ...and try again! | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | 30 | ▶ | if ( exists $Hints{ $subname } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 5 | return $Hints{ $subname }; | |||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | # It's the caller's responsibility to use defaults if desired. | ||||||||||||||||||||||||||||||||||||||||||
| 509 | # This allows on autodie to insist on hints if needed. | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 25 | return; | |||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | 9 | ▶ | sub set_hints_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_hints_for: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | 9 | my ($class, $sub, $hints) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | 9 | ▶ | if (ref $sub) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 8 | $sub = $class->sub_fullname( $sub ); | |||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | 8 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||
| 523 | 8 | ▶ | $sub or Carp::croak("Attempts to set_hints_for unidentifiable subroutine"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | 9 | ▶ | if ($DEBUG) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | warn "autodie::hints: Setting $sub to hints: $hints\n"; | |||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | 9 | $Hints{ $sub } = $hints; | |||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 9 | $class->normalise_hints(\%Hints, $sub); | |||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | __END__ | ||||||||||||||||||||||||||||||||||||||||||