| 1 | package FindApp::Utils::Assert; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use FindApp::Utils::Carp; | ||
| 8 | |||
| 9 | ################################################################# | ||
| 10 | |||
| 11 | sub all_args_defined ; | ||
| 12 | sub bad_args ; | ||
| 13 | sub good_args ; | ||
| 14 | sub panic ; | ||
| 15 | sub subcroak ; | ||
| 16 | sub subcroak_N ; | ||
| 17 | sub validate_args ; | ||
| 18 | |||
| 19 | ################################################################# | ||
| 20 | |||
| 21 | use Exporter qw(import); | ||
| 22 | our $VERSION = v1.0; | ||
| 23 | our @EXPORT_OK = qw( | ||
| 24 | all_args_defined | ||
| 25 | bad_args | ||
| 26 | good_args | ||
| 27 | panic | ||
| 28 | subcroak | ||
| 29 | subcroak_N | ||
| 30 | ); | ||
| 31 | our %EXPORT_TAGS = ( | ||
| 32 | all => \@EXPORT_OK, | ||
| 33 | ); | ||
| 34 | |||
| 35 | ################################################################# | ||
| 36 | |||
| 37 | sub subcroak { | ||
| 38 | my($package, $filename, $line, $sub) = caller(1); | ||
| 39 | my $subname = $sub; | ||
| 40 | my $proto = prototype($sub); | ||
| 41 | $subname .= "($proto)" if defined $proto; | ||
| 42 | |||
| 43 | croak "$subname: @_"; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub subcroak_N { | ||
| 47 | my $frames = shift; | ||
| 48 | my($package, $filename, $line, $sub) = caller($frames); | ||
| 49 | croak "$sub(): @_"; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub panic { | ||
| 54 | confess "panic: @_"; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub validate_args { | ||
| 58 | my($good_condition) = @_; | ||
| 59 | return if $good_condition; | ||
| 60 | my($package, $file, $line, $sub) = caller(2); | ||
| 61 | panic "invalid arguments given to $sub" unless $good_condition; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub bad_args { | ||
| 65 | my($condition) = @_; | ||
| 66 | validate_args !$condition; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub good_args { | ||
| 70 | &validate_args; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub all_args_defined { | ||
| 74 | unless (@_ == grep {defined} @_) { | ||
| 75 | subcroak_N(2, "undefined arguments forbidden"); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | =encoding utf8 | ||
| 83 | |||
| 84 | =head1 NAME | ||
| 85 | |||
| 86 | FindApp::Utils::Assert - FIXME | ||
| 87 | |||
| 88 | =head1 SYNOPSIS | ||
| 89 | |||
| 90 | use FindApp::Utils::Assert; | ||
| 91 | |||
| 92 | =head1 DESCRIPTION | ||
| 93 | |||
| 94 | =head2 Public Methods | ||
| 95 | |||
| 96 | =over | ||
| 97 | |||
| 98 | =item FIXME | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | =head2 Exports | ||
| 103 | |||
| 104 | =over | ||
| 105 | |||
| 106 | =item all_args_defined | ||
| 107 | |||
| 108 | =item bad_args | ||
| 109 | |||
| 110 | =item good_args | ||
| 111 | |||
| 112 | =item panic | ||
| 113 | |||
| 114 | =item subcroak | ||
| 115 | |||
| 116 | =item subcroak_N | ||
| 117 | |||
| 118 | =item validate_args | ||
| 119 | |||
| 120 | =item FIXME | ||
| 121 | |||
| 122 | =back | ||
| 123 | |||
| 124 | =head1 EXAMPLES | ||
| 125 | |||
| 126 | =head1 ENVIRONMENT | ||
| 127 | |||
| 128 | =head1 SEE ALSO | ||
| 129 | |||
| 130 | =over | ||
| 131 | |||
| 132 | =item L<FindApp> | ||
| 133 | |||
| 134 | =back | ||
| 135 | |||
| 136 | =head1 CAVEATS AND PROVISOS | ||
| 137 | |||
| 138 | =head1 BUGS AND LIMITATIONS | ||
| 139 | |||
| 140 | =head1 HISTORY | ||
| 141 | |||
| 142 | =head1 AUTHOR | ||
| 143 | |||
| 144 | Tom Christiansen << <tchrist@perl.com> >> | ||
| 145 | |||
| 146 | =head1 LICENCE AND COPYRIGHT | ||
| 147 | |||
| 148 | Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>. | ||
| 149 | All Rights Reserved. | ||
| 150 | |||
| 151 | This module is free software; you can redistribute it and/or | ||
| 152 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 153 | |||
| 154 | =cut | ||
| 155 |