| 1 | package Eve::Support; | ||||||||||
| 2 | |||||||||||
| 3 | 32 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 32 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 32 | ▶ | use Contextual::Return; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 32 | ▶ | use PadWalker (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 32 | ▶ | use Tie::IxHash; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 32 | ▶ | use Eve::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | B<Eve::Support> - an utility class that houses various helper functions | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | use Eve::Support; | ||||||||||
| 19 | |||||||||||
| 20 | Eve::Support::arguments(\%arg_hash, | ||||||||||
| 21 | my $required_argument, my $optional_argument = 'default value' | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | =head1 SUBROUTINES | ||||||||||
| 25 | |||||||||||
| 26 | =head2 B<arguments()> | ||||||||||
| 27 | |||||||||||
| 28 | The C<arguments()> method makes it easier to specify a list of required | ||||||||||
| 29 | and optional arguments in any other method. It can be used in both | ||||||||||
| 30 | class method calls and usual subroutine calls. | ||||||||||
| 31 | |||||||||||
| 32 | Here is an example in a usual subroutine: | ||||||||||
| 33 | |||||||||||
| 34 | sub usual_subroutine { | ||||||||||
| 35 | my (%arg_hash) = @_; | ||||||||||
| 36 | Eve::Support::arguments(\%arg_hash, | ||||||||||
| 37 | my ($required_argument, $another_required_argument), | ||||||||||
| 38 | my ($optional_argument, $optional_empty_argument) = (1, \undef) | ||||||||||
| 39 | ); | ||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | The same may be done in a class method call: | ||||||||||
| 43 | |||||||||||
| 44 | sub class_method { | ||||||||||
| 45 | my ($self, %arg_hash) = @_; | ||||||||||
| 46 | Eve::Support::arguments(\%arg_hash, | ||||||||||
| 47 | my ($required_argument, $another_required_argument), | ||||||||||
| 48 | my ($optional_argument, $optional_empty_argument) | ||||||||||
| 49 | = ('default', \undef) | ||||||||||
| 50 | ); | ||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | If the function is called in the RVALUE context it skips the | ||||||||||
| 54 | redundancy check and returns the rest of arguments that have not been | ||||||||||
| 55 | processed as a hash reference. | ||||||||||
| 56 | |||||||||||
| 57 | sub foo { | ||||||||||
| 58 | my (%arg_hash) = @_; | ||||||||||
| 59 | my $rest_hash = Eve::Support::arguments( | ||||||||||
| 60 | \%arg_hash, my $bar); | ||||||||||
| 61 | |||||||||||
| 62 | return $rest_hash; | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | Here the call C<foo(bar => 1, baz => 2, bad => 3)> will return the | ||||||||||
| 66 | hash C<{'baz' => 2, 'bad' => 3}>. | ||||||||||
| 67 | |||||||||||
| 68 | =head3 Arguments | ||||||||||
| 69 | |||||||||||
| 70 | =over 4 | ||||||||||
| 71 | |||||||||||
| 72 | =item C<\%arg_hash> | ||||||||||
| 73 | |||||||||||
| 74 | A reference to a hash of arguments that has been passed into the | ||||||||||
| 75 | current method. | ||||||||||
| 76 | |||||||||||
| 77 | =item C<@variable_list> | ||||||||||
| 78 | |||||||||||
| 79 | A list of variables that have to be filled by values from the | ||||||||||
| 80 | incoming C<\%arg_hash>. | ||||||||||
| 81 | |||||||||||
| 82 | =back | ||||||||||
| 83 | |||||||||||
| 84 | =head3 Throws | ||||||||||
| 85 | |||||||||||
| 86 | =over 4 | ||||||||||
| 87 | |||||||||||
| 88 | =item C<Eve::Error::Attribute> | ||||||||||
| 89 | |||||||||||
| 90 | could not get a variable for a named argument, an argument is required | ||||||||||
| 91 | or an argument is redundant. | ||||||||||
| 92 | |||||||||||
| 93 | =back | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 1158 | ▶ | sub arguments : lvalue { | ||||||||
|
Subroutine
arguments: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 98 | 1158 | my $arg_hash = shift; | |||||||||
| 99 | |||||||||||
| 100 | 1158 | foreach my $var (@_) { | |||||||||
| 101 | 3169 | my $name = PadWalker::var_name(1, \$var); | |||||||||
| 102 | 3169 | ▶ | if (not defined($name)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 1 | Eve::Error::Attribute->throw( | |||||||||
| 104 | message => 'Could not get a variable for a named argument'); | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 3168 | $name =~ s/^\$//; | |||||||||
| 108 | |||||||||||
| 109 | 3168 | ▶ | if (exists($arg_hash->{$name})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 3140 | $var = $arg_hash->{$name}; | |||||||||
| 111 | 3140 | delete($arg_hash->{$name}); | |||||||||
| 112 | } else { | ||||||||||
| 113 | 28 | ▶ | if (defined($var)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 27 | ▶ | if ($var eq \undef) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | # Work around default undef value | ||||||||||
| 116 | 16 | $var = undef; | |||||||||
| 117 | } else { | ||||||||||
| 118 | # Leave the value that was assigned | ||||||||||
| 119 | } | ||||||||||
| 120 | } else { | ||||||||||
| 121 | 1 | Eve::Error::Attribute->throw( | |||||||||
| 122 | message => 'Required argument: '.$name); | ||||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 1152 | ▶ | NVALUE { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 128 | 1152 | my @keys = keys(%$arg_hash); | |||||||||
| 129 | 1152 | ▶ | if (@keys) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 2 | Eve::Error::Attribute->throw( | |||||||||
| 131 | message => 'Redundant argument(s): '.join(', ', sort(@keys))); | ||||||||||
| 132 | } | ||||||||||
| 133 | } | ||||||||||
| 134 | 6 | ▶ | RVALUE { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 135 | 6 | $arg_hash; | |||||||||
| 136 | } | ||||||||||
| 137 | 1156 | } | |||||||||
| 138 | |||||||||||
| 139 | =head2 B<unique()> | ||||||||||
| 140 | |||||||||||
| 141 | =head3 Arguments | ||||||||||
| 142 | |||||||||||
| 143 | =over 4 | ||||||||||
| 144 | |||||||||||
| 145 | =item C<list> | ||||||||||
| 146 | |||||||||||
| 147 | =back | ||||||||||
| 148 | |||||||||||
| 149 | =head3 Returns | ||||||||||
| 150 | |||||||||||
| 151 | A list containing only unique elements of the passed list. | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||
| 155 | 3 | ▶ | sub unique { | ||||||||
|
Subroutine
unique: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 156 | 3 | my %arg_hash = @_; | |||||||||
| 157 | 3 | Eve::Support::arguments(\%arg_hash, my $list); | |||||||||
| 158 | |||||||||||
| 159 | 3 | my $unique_list = []; | |||||||||
| 160 | 3 | my $seen_hash = {}; | |||||||||
| 161 | 3 | for my $item (@{$list}) { | |||||||||
| 162 | 12 | ▶ | if (not exists $seen_hash->{$item}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | 10 | push(@{$unique_list}, $item); | |||||||||
| 164 | 10 | $seen_hash->{$item} = 1; | |||||||||
| 165 | } | ||||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | 3 | return $unique_list; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | =head2 B<open()> | ||||||||||
| 172 | |||||||||||
| 173 | =head3 Arguments | ||||||||||
| 174 | |||||||||||
| 175 | =over 4 | ||||||||||
| 176 | |||||||||||
| 177 | =item C<mode> | ||||||||||
| 178 | |||||||||||
| 179 | =item C<file> | ||||||||||
| 180 | |||||||||||
| 181 | =back | ||||||||||
| 182 | |||||||||||
| 183 | =head3 Returns | ||||||||||
| 184 | |||||||||||
| 185 | A filehandle. | ||||||||||
| 186 | |||||||||||
| 187 | =head3 Throws | ||||||||||
| 188 | |||||||||||
| 189 | =over 4 | ||||||||||
| 190 | |||||||||||
| 191 | =item C<Eve::Exception::InputOutput> | ||||||||||
| 192 | |||||||||||
| 193 | in case of a file open error. | ||||||||||
| 194 | |||||||||||
| 195 | =back | ||||||||||
| 196 | |||||||||||
| 197 | =cut | ||||||||||
| 198 | |||||||||||
| 199 | 2 | ▶ | sub open { | ||||||||
|
Subroutine
open: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 200 | 2 | my %arg_hash = @_; | |||||||||
| 201 | 2 | Eve::Support::arguments(\%arg_hash, my ($mode, $file)); | |||||||||
| 202 | |||||||||||
| 203 | 2 | ▶ | open(my $filehandle, $mode, $file) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 204 | Eve::Exception::InputOutput->throw( | ||||||||||
| 205 | message => "Error occured when opening the file '$file': ".$!); | ||||||||||
| 206 | |||||||||||
| 207 | 2 | return $filehandle; | |||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | =head2 B<indexed_hash()> | ||||||||||
| 211 | |||||||||||
| 212 | =head3 Arguments | ||||||||||
| 213 | |||||||||||
| 214 | Key-value pair list. | ||||||||||
| 215 | |||||||||||
| 216 | =head3 Returns | ||||||||||
| 217 | |||||||||||
| 218 | A hash tied to L<Tie::IxHash>. | ||||||||||
| 219 | |||||||||||
| 220 | =cut | ||||||||||
| 221 | |||||||||||
| 222 | 5 | ▶ | sub indexed_hash { | ||||||||
|
Subroutine
indexed_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 223 | 5 | tie(my %hash, 'Tie::IxHash', @_); | |||||||||
| 224 | |||||||||||
| 225 | 5 | return \%hash; | |||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | =head2 B<trim()> | ||||||||||
| 229 | |||||||||||
| 230 | =head3 Arguments | ||||||||||
| 231 | |||||||||||
| 232 | A string. | ||||||||||
| 233 | |||||||||||
| 234 | =head3 Returns | ||||||||||
| 235 | |||||||||||
| 236 | Trimmed string. | ||||||||||
| 237 | |||||||||||
| 238 | =head3 Throws | ||||||||||
| 239 | |||||||||||
| 240 | =over 4 | ||||||||||
| 241 | |||||||||||
| 242 | =item C<Eve::Exception::Value> | ||||||||||
| 243 | |||||||||||
| 244 | if the triming value is undefined. | ||||||||||
| 245 | |||||||||||
| 246 | =back | ||||||||||
| 247 | |||||||||||
| 248 | =cut | ||||||||||
| 249 | |||||||||||
| 250 | 5 | ▶ | sub trim { | ||||||||
|
Subroutine
trim: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 251 | 5 | my %arg_hash = @_; | |||||||||
| 252 | 5 | Eve::Support::arguments(\%arg_hash, my $string); | |||||||||
| 253 | |||||||||||
| 254 | 5 | ▶ | if (not defined $string) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 255 | 1 | Eve::Error::Value->throw( | |||||||||
| 256 | message => 'Trimming value must be defined'); | ||||||||||
| 257 | } | ||||||||||
| 258 | |||||||||||
| 259 | 4 | $string =~ s/^\s+//; | |||||||||
| 260 | 4 | $string =~ s/\s+$//; | |||||||||
| 261 | |||||||||||
| 262 | 4 | return $string; | |||||||||
| 263 | } | ||||||||||
| 264 | |||||||||||
| 265 | =head1 SEE ALSO | ||||||||||
| 266 | |||||||||||
| 267 | =over 4 | ||||||||||
| 268 | |||||||||||
| 269 | =item L<Eve::Exception> | ||||||||||
| 270 | |||||||||||
| 271 | =back | ||||||||||
| 272 | |||||||||||
| 273 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 274 | |||||||||||
| 275 | Copyright 2012 Igor Zinovyev. | ||||||||||
| 276 | |||||||||||
| 277 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 278 | under the terms of either: the GNU General Public License as published | ||||||||||
| 279 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 280 | |||||||||||
| 281 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 282 | |||||||||||
| 283 | |||||||||||
| 284 | =head1 AUTHOR | ||||||||||
| 285 | |||||||||||
| 286 | =over 4 | ||||||||||
| 287 | |||||||||||
| 288 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||||||||||
| 289 | |||||||||||
| 290 | =back | ||||||||||
| 291 | |||||||||||
| 292 | =cut | ||||||||||
| 293 | |||||||||||
| 294 | 32 | 1; | |||||||||