| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | package Bio::EnsEMBL::Utils::Scalar; | ||
| 21 | $Bio::EnsEMBL::Utils::Scalar::VERSION = '114.0.0_50'; # TRIAL | ||
| 22 | $Bio::EnsEMBL::Utils::Scalar::VERSION = '114.0.050'; | ||
| 23 | =pod | ||
| 24 | |||
| 25 | |||
| 26 | =head1 CONTACT | ||
| 27 | |||
| 28 | Please email comments or questions to the public Ensembl | ||
| 29 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 30 | |||
| 31 | Questions may also be sent to the Ensembl help desk at | ||
| 32 | <http://www.ensembl.org/Help/Contact>. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | =pod | ||
| 37 | |||
| 38 | =head1 NAME | ||
| 39 | |||
| 40 | Bio::EnsEMBL::Utils::Scalar | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | use Bio::EnsEMBL::Utils::Scalar qw(check_ref assert_ref wrap_array check_ref_can assert_ref_can assert_numeric assert_integer scope_guard); | ||
| 45 | |||
| 46 | check_ref([], 'ARRAY'); # Will return true | ||
| 47 | check_ref({}, 'ARRAY'); # Will return false | ||
| 48 | check_ref($dba, 'Bio::EnsEMBL::DBSQL::DBAdaptor'); #Returns true if $dba is a DBAdaptor | ||
| 49 | |||
| 50 | # Returns true if all array contents are of the given type | ||
| 51 | check_array_contents([$dba], 'Bio::EnsEMBL::DBSQL::DBAdaptor'); | ||
| 52 | |||
| 53 | assert_ref([], 'ARRAY'); #Returns true | ||
| 54 | assert_ref({}, 'ARRAY'); #throws an exception | ||
| 55 | assert_ref($dba, 'Bio::EnsEMBL::Gene'); #throws an exception if $dba is not a Gene | ||
| 56 | |||
| 57 | # Throws an exception if all array contents are not of the given type | ||
| 58 | assert_array_contents([$dba], 'Bio::EnsEMBL::Gene'); #throws an exception if $dba is not a Gene | ||
| 59 | |||
| 60 | wrap_array([]); #Returns the same reference | ||
| 61 | wrap_array($a); #Returns [$a] if $a was not an array | ||
| 62 | wrap_array(undef); #Returns [] since incoming was undefined | ||
| 63 | wrap_array(); #Returns [] since incoming was empty (therefore undefined) | ||
| 64 | |||
| 65 | check_ref_can([], 'dbID'); #returns false as ArrayRef is not blessed | ||
| 66 | check_ref_can($gene, 'dbID'); #returns true as Gene should implement dbID() | ||
| 67 | check_ref_can(undef); #Throws an exception as we gave no method to test | ||
| 68 | |||
| 69 | assert_ref_can([], 'dbID'); #throws an exception since ArrayRef is not blessed | ||
| 70 | assert_ref_can($gene, 'dbID'); #returns true if gene implements dbID() | ||
| 71 | assert_ref_can(undef); #Throws an exception as we gave no method to test | ||
| 72 | |||
| 73 | asssert_integer(1, 'dbID'); #Passes | ||
| 74 | asssert_integer(1.1, 'dbID'); #Fails | ||
| 75 | asssert_numeric(1E-11, 'dbID'); #Passes | ||
| 76 | asssert_numeric({}, 'dbID'); #Fails | ||
| 77 | |||
| 78 | #Scope guards | ||
| 79 | my $v = 'wibble'; | ||
| 80 | { | ||
| 81 | #Build a guard to reset $v to wibble | ||
| 82 | my $guard = scope_guard(sub { $v = 'wibble'}); | ||
| 83 | $v = 'wobble'; | ||
| 84 | warn $v; # prints wobble | ||
| 85 | } | ||
| 86 | # $guard is out of scope; sub is triggered and $v is reset | ||
| 87 | warn $v; # prints wibble | ||
| 88 | |||
| 89 | #Tags are also available for exporting | ||
| 90 | use Bio::EnsEMBL::Utils::Scalar qw(:assert); # brings in all assert methods | ||
| 91 | use Bio::EnsEMBL::Utils::Scalar qw(:check); #brings in all check methods | ||
| 92 | use Bio::EnsEMBL::Utils::Scalar qw(:array); #brings in wrap_array | ||
| 93 | use Bio::EnsEMBL::Utils::Scalar qw(:all); #import all methods | ||
| 94 | |||
| 95 | =head1 DESCRIPTION | ||
| 96 | |||
| 97 | A collection of subroutines aimed to helping Scalar based operations | ||
| 98 | |||
| 99 | =head1 METHODS | ||
| 100 | |||
| 101 | See subroutines. | ||
| 102 | |||
| 103 | =head1 MAINTAINER | ||
| 104 | |||
| 105 | $Author$ | ||
| 106 | |||
| 107 | =head1 VERSION | ||
| 108 | |||
| 109 | $Revision$ | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | use strict; | ||
| 114 | use warnings; | ||
| 115 | |||
| 116 | # | ||
| 117 | # Interface with some of the module function XS reimplementation | ||
| 118 | # | ||
| 119 | # If Bio::EnsEMBL::XS is installed, assign the function glob to | ||
| 120 | # the XS counterpart, otherwise assign to the original function | ||
| 121 | # | ||
| 122 | BEGIN { | ||
| 123 | |||
| 124 | if (eval { require Bio::EnsEMBL::XS; 1 }) { | ||
| 125 | *check_ref = \&Bio::EnsEMBL::XS::Utils::Scalar::check_ref; | ||
| 126 | *assert_ref = \&Bio::EnsEMBL::XS::Utils::Scalar::assert_ref; | ||
| 127 | # *assert_numeric = \&Bio::EnsEMBL::XS::Utils::Scalar::assert_numeric; | ||
| 128 | # *assert_integer = \&Bio::EnsEMBL::XS::Utils::Scalar::assert_integer; | ||
| 129 | } else { | ||
| 130 | *check_ref = \&check_ref_pp; | ||
| 131 | *assert_ref = \&assert_ref_pp; | ||
| 132 | # *assert_numeric = \&assert_numeric_pp; | ||
| 133 | # *assert_integer = \&assert_integer_pp; | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | *assert_numeric = \&assert_numeric_pp; | ||
| 138 | *assert_integer = \&assert_integer_pp; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | use base qw(Exporter); | ||
| 143 | |||
| 144 | our %EXPORT_TAGS; | ||
| 145 | our @EXPORT_OK; | ||
| 146 | |||
| 147 | @EXPORT_OK = qw( | ||
| 148 | check_ref check_ref_can check_array_contents check_hash_contents | ||
| 149 | assert_ref assert_ref_can assert_numeric assert_integer assert_boolean assert_strand assert_file_handle assert_array_contents assert_hash_contents | ||
| 150 | wrap_array | ||
| 151 | scope_guard | ||
| 152 | split_array | ||
| 153 | ); | ||
| 154 | %EXPORT_TAGS = ( | ||
| 155 | assert => [qw(assert_ref assert_ref_can assert_integer assert_numeric assert_boolean assert_strand assert_file_handle assert_array_contents assert_hash_contents)], | ||
| 156 | check => [qw(check_ref check_ref_can check_array_contents check_hash_contents)], | ||
| 157 | array => [qw/wrap_array split_array/], | ||
| 158 | all => [@EXPORT_OK] | ||
| 159 | ); | ||
| 160 | |||
| 161 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 162 | use Scalar::Util qw(blessed looks_like_number openhandle); | ||
| 163 | |||
| 164 | our $ASSERTIONS = 1; | ||
| 165 | |||
| 166 | =head2 check_ref_pp() | ||
| 167 | |||
| 168 | Arg [1] : The reference to check | ||
| 169 | Arg [2] : The type we expect | ||
| 170 | Description : A subroutine which checks to see if the given object/ref is | ||
| 171 | what you expect. If you give it a blessed reference then it | ||
| 172 | will perform an isa() call on the object after the defined | ||
| 173 | tests. If it is a plain reference then it will use ref(). | ||
| 174 | |||
| 175 | An undefined value will return a false. | ||
| 176 | Returntype : Boolean indicating if the reference was the type we | ||
| 177 | expect | ||
| 178 | Example : my $ok = check_ref([], 'ARRAY'); | ||
| 179 | Exceptions : If the expected type was not set | ||
| 180 | Status : Stable | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub check_ref_pp { | ||
| 185 | my ($ref, $expected) = @_; | ||
| 186 | throw('No expected type given') if ! defined $expected; | ||
| 187 | if(defined $ref) { | ||
| 188 | if(blessed($ref)) { | ||
| 189 | return 1 if $ref->isa($expected); | ||
| 190 | } | ||
| 191 | else { | ||
| 192 | my $ref_ref_type = ref($ref); | ||
| 193 | return 1 if defined $ref_ref_type && $ref_ref_type eq $expected; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | return 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 assert_ref_pp() | ||
| 200 | |||
| 201 | Arg [1] : The reference to check | ||
| 202 | Arg [2] : The type we expect | ||
| 203 | Arg [3] : The attribute name you are asserting; not required but allows | ||
| 204 | for more useful error messages to be generated. Defaults to | ||
| 205 | C<-Unknown->. | ||
| 206 | Description : A subroutine which checks to see if the given object/ref is | ||
| 207 | what you expect. This behaves in an identical manner as | ||
| 208 | C<check_ref()> does except this will raise exceptions when | ||
| 209 | the values do not match rather than returning a boolean | ||
| 210 | indicating the situation. | ||
| 211 | |||
| 212 | Undefs cause exception circumstances. | ||
| 213 | |||
| 214 | You can turn assertions off by using the global variable | ||
| 215 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 216 | Returntype : Boolean; true if we managed to get to the return | ||
| 217 | Example : assert_ref([], 'ARRAY'); | ||
| 218 | Exceptions : If the expected type was not set and if the given reference | ||
| 219 | was not assignable to the expected value | ||
| 220 | Status : Stable | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub assert_ref_pp { | ||
| 225 | my ($ref, $expected, $attribute_name) = @_; | ||
| 226 | return 1 unless $ASSERTIONS; | ||
| 227 | $attribute_name ||= '-Unknown-'; | ||
| 228 | throw('No expected type given') if ! defined $expected; | ||
| 229 | my $class = ref($ref); | ||
| 230 | throw("The given reference for attribute $attribute_name was undef. Expected '$expected'") unless defined $ref; | ||
| 231 | throw("Asking for the type of the attribute $attribute_name produced no type; check it is a reference. Expected '$expected'") unless $class; | ||
| 232 | if(blessed($ref)) { | ||
| 233 | throw("${attribute_name}'s type '${class}' is not an ISA of '${expected}'") if ! $ref->isa($expected); | ||
| 234 | } | ||
| 235 | else { | ||
| 236 | throw("$attribute_name was expected to be '${expected}' but was '${class}'") if $expected ne $class; | ||
| 237 | } | ||
| 238 | return 1; | ||
| 239 | } | ||
| 240 | |||
| 241 | =head2 assert_array_contents | ||
| 242 | |||
| 243 | Arg [1] : ArrayRef references to check | ||
| 244 | Arg [2] : The type we expect | ||
| 245 | Arg [3] : The attribute name you are asserting; not required but allows | ||
| 246 | for more useful error messages to be generated. Defaults to | ||
| 247 | C<-Unknown->. | ||
| 248 | Description : A subroutine which checks to see if the given objects/refs are | ||
| 249 | what you expect. This behaves in an identical manner as | ||
| 250 | C<assert_ref> does works on an array ref of references | ||
| 251 | |||
| 252 | You can turn assertions off by using the global variable | ||
| 253 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 254 | Returntype : Boolean; true if we managed to get to the return | ||
| 255 | Example : assert_array_contents([[],[],[]], 'ARRAY'); | ||
| 256 | Exceptions : Throws is references argument is not an ArrayRef, also | ||
| 257 | if the expected type was not set and if the given reference | ||
| 258 | was not assignable to the expected value. | ||
| 259 | Status : Stable | ||
| 260 | |||
| 261 | =cut | ||
| 262 | |||
| 263 | sub assert_array_contents { | ||
| 264 | my ($array, $expected, $attribute_name) = @_; | ||
| 265 | return 1 unless $ASSERTIONS; | ||
| 266 | throw('No expected type given') if ! defined $expected; | ||
| 267 | $attribute_name ||= '-Unknown-'; | ||
| 268 | assert_ref($array, 'ARRAY', $attribute_name); | ||
| 269 | my $count = scalar(@{$array}); | ||
| 270 | for(my $i = 0; $i<$count; $i++) { | ||
| 271 | my $ref = $array->[$i]; | ||
| 272 | my $class = ref($ref); | ||
| 273 | throw("The given reference for attribute $attribute_name was undef (at position ${i}). Expected '$expected'") unless defined $ref; | ||
| 274 | throw("Asking for the type of the attribute $attribute_name produced no type; check it is a reference (at position ${i}). Expected '$expected'") unless $class; | ||
| 275 | if(blessed($ref)) { | ||
| 276 | throw("${attribute_name}'s type '${class}' is not an ISA of '${expected}' (at position ${i})") if ! $ref->isa($expected); | ||
| 277 | } | ||
| 278 | else { | ||
| 279 | throw("$attribute_name was expected to be '${expected}' but was '${class}' (at position ${i})") if $expected ne $class; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | return 1; | ||
| 283 | } | ||
| 284 | |||
| 285 | =head2 check_array_contents | ||
| 286 | |||
| 287 | Arg [1] : ArrayRef references to check | ||
| 288 | Arg [2] : The type we expect | ||
| 289 | Arg [3] : The attribute name you are asserting; not required but allows | ||
| 290 | for more useful error messages to be generated. Defaults to | ||
| 291 | C<-Unknown->. | ||
| 292 | Description : A subroutine which checks to see if the given objects/refs are | ||
| 293 | what you expect. | ||
| 294 | Returntype : Boolean; true if all contents were as expected | ||
| 295 | Example : check_array_contents([[],[],[]], 'ARRAY'); | ||
| 296 | Exceptions : Thrown if no type was given | ||
| 297 | Status : Stable | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | sub check_array_contents { | ||
| 302 | my ($array, $expected, $attribute_name) = @_; | ||
| 303 | return 0 if ! check_ref($array, 'ARRAY'); | ||
| 304 | throw('No expected type given') if ! defined $expected; | ||
| 305 | my $contents_ok = 1; | ||
| 306 | my $count = scalar(@{$array}); | ||
| 307 | for(my $i = 0; $i<$count; $i++) { | ||
| 308 | my $ref = $array->[$i]; | ||
| 309 | if(!$ref) { | ||
| 310 | $contents_ok = 0; | ||
| 311 | last; | ||
| 312 | } | ||
| 313 | my $class = ref($ref); | ||
| 314 | if(!$class) { | ||
| 315 | $contents_ok = 0; | ||
| 316 | last; | ||
| 317 | } | ||
| 318 | if(blessed($ref)) { | ||
| 319 | if(! $ref->isa($expected)) { | ||
| 320 | $contents_ok = 0; | ||
| 321 | last; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | elsif($expected ne $class) { | ||
| 325 | $contents_ok = 0; | ||
| 326 | last; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | return $contents_ok; | ||
| 330 | } | ||
| 331 | |||
| 332 | =head2 assert_hash_contents | ||
| 333 | |||
| 334 | Arg [1] : HashRef references to check | ||
| 335 | Arg [2] : The type we expect | ||
| 336 | Arg [3] : The attribute name you are asserting; not required but allows | ||
| 337 | for more useful error messages to be generated. Defaults to | ||
| 338 | C<-Unknown->. | ||
| 339 | Description : A subroutine which checks to see if the given objects/refs are | ||
| 340 | what you expect. This behaves in an identical manner as | ||
| 341 | C<assert_ref> does works on a HashRef of references. Hash keys | ||
| 342 | are always Strings so do not need asserting. | ||
| 343 | |||
| 344 | You can turn assertions off by using the global variable | ||
| 345 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 346 | Returntype : Boolean; true if we managed to get to the return | ||
| 347 | Example : assert_hash_contents({a => [], b => []}, 'ARRAY'); | ||
| 348 | Exceptions : Throws is references argument is not an ArrayRef, also | ||
| 349 | if the expected type was not set and if the given reference | ||
| 350 | was not assignable to the expected value. | ||
| 351 | Status : Stable | ||
| 352 | |||
| 353 | =cut | ||
| 354 | |||
| 355 | sub assert_hash_contents { | ||
| 356 | my ($hash, $expected, $attribute_name) = @_; | ||
| 357 | return 1 unless $ASSERTIONS; | ||
| 358 | throw('No expected type given') if ! defined $expected; | ||
| 359 | $attribute_name ||= '-Unknown-'; | ||
| 360 | assert_ref($hash, 'HASH', $attribute_name); | ||
| 361 | my @keys = keys %{$hash}; | ||
| 362 | while(my $key = shift @keys) { | ||
| 363 | my $ref = $hash->{$key}; | ||
| 364 | my $class = ref($ref); | ||
| 365 | throw("The given reference for attribute $attribute_name was undef (with key ${key}). Expected '$expected'") unless defined $ref; | ||
| 366 | throw("Asking for the type of the attribute $attribute_name produced no type; check it is a reference (with key ${key}). Expected '$expected'") unless $class; | ||
| 367 | if(blessed($ref)) { | ||
| 368 | throw("${attribute_name}'s type '${class}' is not an ISA of '${expected}' (with key ${key})") if ! $ref->isa($expected); | ||
| 369 | } | ||
| 370 | else { | ||
| 371 | throw("$attribute_name was expected to be '${expected}' but was '${class}' (with key ${key})") if $expected ne $class; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | return 1; | ||
| 375 | } | ||
| 376 | |||
| 377 | =head2 check_hash_contents | ||
| 378 | |||
| 379 | Arg [1] : HashRef references to check | ||
| 380 | Arg [2] : The type we expect | ||
| 381 | Arg [3] : The attribute name you are asserting; not required but allows | ||
| 382 | for more useful error messages to be generated. Defaults to | ||
| 383 | C<-Unknown->. | ||
| 384 | Description : A subroutine which checks to see if the given objects/refs are | ||
| 385 | what you expect. | ||
| 386 | Returntype : Boolean; true if all contents were as expected | ||
| 387 | Example : check_hash_contents({a => [], b => []}, 'ARRAY'); | ||
| 388 | Exceptions : Thrown if no type was given | ||
| 389 | Status : Stable | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | sub check_hash_contents { | ||
| 394 | my ($hash, $expected, $attribute_name) = @_; | ||
| 395 | throw('No expected type given') if ! defined $expected; | ||
| 396 | return 0 if ! check_ref($hash, 'HASH'); | ||
| 397 | my $contents_ok = 1; | ||
| 398 | my @keys = keys %{$hash}; | ||
| 399 | while(my $key = shift @keys) { | ||
| 400 | my $ref = $hash->{$key}; | ||
| 401 | if(!$ref) { | ||
| 402 | $contents_ok = 0; | ||
| 403 | last; | ||
| 404 | } | ||
| 405 | my $class = ref($ref); | ||
| 406 | if(!$class) { | ||
| 407 | $contents_ok = 0; | ||
| 408 | last; | ||
| 409 | } | ||
| 410 | if(blessed($ref)) { | ||
| 411 | if(! $ref->isa($expected)) { | ||
| 412 | $contents_ok = 0; | ||
| 413 | last; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | elsif($expected ne $class) { | ||
| 417 | $contents_ok = 0; | ||
| 418 | last; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | return $contents_ok; | ||
| 422 | } | ||
| 423 | |||
| 424 | =head2 wrap_array() | ||
| 425 | |||
| 426 | Arg : The reference we want to wrap in an array | ||
| 427 | Description : Takes in a reference and returns either the reference if it | ||
| 428 | was already an array, the reference wrapped in an array or | ||
| 429 | an empty array (if the given value was undefined). | ||
| 430 | Returntype : Array Reference | ||
| 431 | Example : my $a = wrap_array($input); | ||
| 432 | Exceptions : None | ||
| 433 | Status : Stable | ||
| 434 | |||
| 435 | =cut | ||
| 436 | |||
| 437 | sub wrap_array { | ||
| 438 | my ($incoming_reference) = @_; | ||
| 439 | if(defined $incoming_reference) { | ||
| 440 | if(check_ref($incoming_reference, 'ARRAY')) { | ||
| 441 | return $incoming_reference; | ||
| 442 | } | ||
| 443 | else { | ||
| 444 | return [$incoming_reference]; | ||
| 445 | } | ||
| 446 | } | ||
| 447 | return []; | ||
| 448 | } | ||
| 449 | |||
| 450 | =head2 check_ref_can | ||
| 451 | |||
| 452 | Arg [1] : The reference to check | ||
| 453 | Arg [2] : The method we expect to run | ||
| 454 | Description : A subroutine which checks to see if the given object/ref is | ||
| 455 | implements the given method. This is very similar to the | ||
| 456 | functionality given by C<UNIVERSAL::can()> but works | ||
| 457 | by executing C<can()> on the object meaning we consult the | ||
| 458 | object's potentially overriden version rather than Perl's | ||
| 459 | default mechanism. | ||
| 460 | Returntype : CodeRef | ||
| 461 | Example : check_ref_can($gene, 'dbID'); | ||
| 462 | Exceptions : If the expected type was not set. | ||
| 463 | Status : Stable | ||
| 464 | |||
| 465 | =cut | ||
| 466 | |||
| 467 | sub check_ref_can { | ||
| 468 | my ($ref, $method) = @_; | ||
| 469 | throw('No method given') if ! defined $method; | ||
| 470 | return unless defined $ref && blessed($ref); | ||
| 471 | return $ref->can($method); | ||
| 472 | } | ||
| 473 | |||
| 474 | =head2 assert_ref_can | ||
| 475 | |||
| 476 | Arg [1] : The reference to check | ||
| 477 | Arg [2] : The method we expect to run | ||
| 478 | Arg [3] : The attribute name you are asserting; not required but allows | ||
| 479 | for more useful error messages to be generated. Defaults to | ||
| 480 | C<-Unknown->. | ||
| 481 | Description : A subroutine which checks to see if the given object/ref is | ||
| 482 | implements the given method. Will throw exceptions. | ||
| 483 | |||
| 484 | You can turn assertions off by using the global variable | ||
| 485 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 486 | Returntype : Boolean; true if we managed to get to the return | ||
| 487 | Example : assert_ref_can($gene, 'dbID'); | ||
| 488 | Exceptions : If the reference is not defined, if the object does not | ||
| 489 | implement the given method and if no method was given to check | ||
| 490 | Status : Stable | ||
| 491 | |||
| 492 | =cut | ||
| 493 | |||
| 494 | sub assert_ref_can { | ||
| 495 | my ($ref, $method, $attribute_name) = @_; | ||
| 496 | return 1 unless $ASSERTIONS; | ||
| 497 | $attribute_name ||= '-Unknown-'; | ||
| 498 | throw('No method given') if ! defined $method; | ||
| 499 | throw "The given reference $attribute_name is not defined. Expected method '$method'" unless defined $ref; | ||
| 500 | throw "The given reference $attribute_name is not blessed. Expected method '$method'" unless blessed($ref); | ||
| 501 | if(! $ref->can($method)) { | ||
| 502 | my $str_ref = ref($ref); | ||
| 503 | throw sprintf(q{The given blessed reference '%s' for attribute '%s' does not implement the method '%s'}, $str_ref, $attribute_name, $method); | ||
| 504 | } | ||
| 505 | return 1; | ||
| 506 | } | ||
| 507 | |||
| 508 | =head2 assert_numeric_pp | ||
| 509 | |||
| 510 | Arg [1] : The Scalar to check | ||
| 511 | Arg [2] : The attribute name you are asserting; not required but allows | ||
| 512 | for more useful error messages to be generated. Defaults to | ||
| 513 | C<-Unknown->. | ||
| 514 | Description : A subroutine which checks to see if the given scalar is | ||
| 515 | number or not. If not then we raise exceptions detailing why | ||
| 516 | |||
| 517 | You can turn assertions off by using the global variable | ||
| 518 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 519 | Returntype : Boolean; true if we had a numeric otherwise we signal failure | ||
| 520 | via exceptions | ||
| 521 | Example : assert_numeric(1, 'dbID'); | ||
| 522 | Exceptions : If the Scalar is not defined, if the Scalar was blessed and | ||
| 523 | if the value was not a number | ||
| 524 | Status : Stable | ||
| 525 | |||
| 526 | =cut | ||
| 527 | |||
| 528 | sub assert_numeric_pp { | ||
| 529 | my ($integer, $attribute_name) = @_; | ||
| 530 | return 1 unless $ASSERTIONS; | ||
| 531 | $attribute_name ||= '-Unknown-'; | ||
| 532 | throw "$attribute_name attribute is undefined. Expected a number" if ! defined $integer; | ||
| 533 | throw "The given attribute $attribute_name is blessed; cannot work with blessed values. Expected a number" if blessed($integer); | ||
| 534 | if(! looks_like_number($integer)) { | ||
| 535 | throw "Attribute $attribute_name was not a number"; | ||
| 536 | } | ||
| 537 | return 1; | ||
| 538 | } | ||
| 539 | |||
| 540 | =head2 assert_integer_pp | ||
| 541 | |||
| 542 | Arg [1] : The Scalar to check | ||
| 543 | Arg [2] : The attribute name you are asserting; not required but allows | ||
| 544 | for more useful error messages to be generated. Defaults to | ||
| 545 | C<-Unknown->. | ||
| 546 | Description : A subroutine which checks to see if the given scalar is | ||
| 547 | a whole integer; we delegate to L<assert_numeric> for number | ||
| 548 | checking. | ||
| 549 | |||
| 550 | You can turn assertions off by using the global variable | ||
| 551 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 552 | Returntype : Boolean; true if we had a numeric otherwise we signal failure | ||
| 553 | via exceptions | ||
| 554 | Example : assert_integer(1, 'dbID'); | ||
| 555 | Exceptions : See L<assert_numeric> and we raise exceptions if the value | ||
| 556 | was not a whole integer | ||
| 557 | Status : Stable | ||
| 558 | |||
| 559 | =cut | ||
| 560 | |||
| 561 | sub assert_integer_pp { | ||
| 562 | my ($integer, $attribute_name) = @_; | ||
| 563 | return 1 unless $ASSERTIONS; | ||
| 564 | $attribute_name ||= '-Unknown-'; | ||
| 565 | throw "$attribute_name attribute is undefined. Expected an Integer" if ! defined $integer; | ||
| 566 | throw "The given attribute $attribute_name is blessed; cannot work with blessed values. Expected an Integer" if blessed($integer); | ||
| 567 | if(! looks_like_number($integer)) { | ||
| 568 | throw "Attribute $attribute_name was not a number. Expected an Integer"; | ||
| 569 | } | ||
| 570 | if($integer != int($integer)) { | ||
| 571 | throw "Attribute $attribute_name was a number but not an Integer"; | ||
| 572 | } | ||
| 573 | return 1; | ||
| 574 | } | ||
| 575 | |||
| 576 | =head2 assert_boolean | ||
| 577 | |||
| 578 | Arg [1] : The Scalar to check | ||
| 579 | Arg [2] : The attribute name you are asserting; not required but allows | ||
| 580 | for more useful error messages to be generated. Defaults to | ||
| 581 | C<-Unknown->. | ||
| 582 | Description : A subroutine which checks to see if the given scalar is | ||
| 583 | a boolean i.e. value is set to C<1> or C<0> | ||
| 584 | |||
| 585 | You can turn assertions off by using the global variable | ||
| 586 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 587 | Returntype : Boolean; true if we were given a boolean otherwise we signal | ||
| 588 | failure via exceptions | ||
| 589 | Example : assert_boolean(1, 'is_circular'); | ||
| 590 | Exceptions : See L<assert_integer> and we raise exceptions if the value | ||
| 591 | was not equal to the 2 valid states | ||
| 592 | Status : Stable | ||
| 593 | |||
| 594 | =cut | ||
| 595 | |||
| 596 | sub assert_boolean { | ||
| 597 | my ($boolean, $attribute_name) = @_; | ||
| 598 | return 1 unless $ASSERTIONS; | ||
| 599 | $attribute_name ||= '-Unknown-'; | ||
| 600 | throw "$attribute_name attribute is undefined. Expected a boolean" if ! defined $boolean; | ||
| 601 | throw "The given attribute $attribute_name is blessed; cannot work with blessed values. Expected an Integer" if blessed($boolean); | ||
| 602 | if(! looks_like_number($boolean)) { | ||
| 603 | throw "Attribute $attribute_name was not a number. Expected a boolean"; | ||
| 604 | } | ||
| 605 | if($boolean != 0 && $boolean != 1) { | ||
| 606 | throw "Attribute $attribute_name was an invalid boolean. Expected: 1 or 0. Got: $boolean"; | ||
| 607 | } | ||
| 608 | return 1; | ||
| 609 | } | ||
| 610 | |||
| 611 | =head2 assert_strand | ||
| 612 | |||
| 613 | Arg [1] : The Scalar to check | ||
| 614 | Arg [2] : The attribute name you are asserting; not required but allows | ||
| 615 | for more useful error messages to be generated. Defaults to | ||
| 616 | C<-Unknown->. | ||
| 617 | Description : A subroutine which checks to see if the given scalar is | ||
| 618 | a whole integer and if the value is set to C<1>, C<0> or C<-1> | ||
| 619 | |||
| 620 | You can turn assertions off by using the global variable | ||
| 621 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 622 | Returntype : Boolean; true if we had a strand integer otherwise we signal | ||
| 623 | failure via exceptions | ||
| 624 | Example : assert_strand(1, 'strand'); | ||
| 625 | Exceptions : See L<assert_integer> and we raise exceptions if the value | ||
| 626 | was not equal to the 3 valid states | ||
| 627 | Status : Stable | ||
| 628 | |||
| 629 | =cut | ||
| 630 | |||
| 631 | sub assert_strand { | ||
| 632 | my ($strand, $attribute_name) = @_; | ||
| 633 | return 1 unless $ASSERTIONS; | ||
| 634 | $attribute_name ||= '-Unknown-'; | ||
| 635 | throw "$attribute_name attribute is undefined. Expected: 1, 0 or -1" if ! defined $strand; | ||
| 636 | throw "The given attribute $attribute_name is blessed; cannot work with blessed values. Expected: 1, 0 or -1" if blessed($strand); | ||
| 637 | if(! looks_like_number($strand)) { | ||
| 638 | throw "Attribute $attribute_name was not a number. Expected: 1, 0 or -1"; | ||
| 639 | } | ||
| 640 | if($strand != -1 && $strand != 0 && $strand ne 1) { | ||
| 641 | throw "Attribute $attribute_name was an invalid strand. Expected: 1, 0 or -1. Got: $strand"; | ||
| 642 | } | ||
| 643 | return 1; | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | =head2 assert_file_handle | ||
| 648 | |||
| 649 | Arg [1] : The Scalar to check | ||
| 650 | Arg [2] : The attribute name you are asserting; not required but allows | ||
| 651 | for more useful error messages to be generated. Defaults to | ||
| 652 | C<-Unknown->. | ||
| 653 | Description : A subroutine which checks to see if the given scalar is | ||
| 654 | actually a file handle. This will handle those which are Glob | ||
| 655 | references and those which inherit from C<IO::Handle>. It will | ||
| 656 | also cope with a blessed Glob reference. | ||
| 657 | |||
| 658 | You can turn assertions off by using the global variable | ||
| 659 | $Bio::EnsEMBL::Utils::Scalar::ASSERTIONS = 0 | ||
| 660 | Returntype : Boolean; | ||
| 661 | Example : assert_file_handle($fh, '-FILE_HANDLE'); | ||
| 662 | Exceptions : Raised if not defined, not a reference and was not a | ||
| 663 | GLOB or did not inherit from IO::Handle | ||
| 664 | Status : Stable | ||
| 665 | |||
| 666 | =cut | ||
| 667 | |||
| 668 | sub assert_file_handle { | ||
| 669 | my ($file_handle, $attribute_name) = @_; | ||
| 670 | return 1 unless $ASSERTIONS; | ||
| 671 | $attribute_name ||= '-Unknown-'; | ||
| 672 | throw "Attribute $attribute_name was undefined. Expected a FileHandle" if ! defined $file_handle; | ||
| 673 | my $ref = ref($file_handle); | ||
| 674 | throw "Attribute $attribute_name was not a reference. Got: $file_handle. Expected a FileHandle" if ! $ref; | ||
| 675 | if(!openhandle($file_handle)) { | ||
| 676 | if(blessed($file_handle)) { | ||
| 677 | if(! $file_handle->isa('IO::Handle')) { | ||
| 678 | throw "Attribute $attribute_name was blessed but did not inherit from IO::Handle. Ref was: $ref"; | ||
| 679 | } | ||
| 680 | } | ||
| 681 | else { | ||
| 682 | throw "Attribute $attribute_name was not a file handle. Ref was: $ref"; | ||
| 683 | } | ||
| 684 | } | ||
| 685 | return 1; | ||
| 686 | } | ||
| 687 | |||
| 688 | =head2 split_array | ||
| 689 | |||
| 690 | Arg [1] : Integer Maximum size of an array produced | ||
| 691 | Arg [2] : ArrayRef The array to split | ||
| 692 | Description : Takes an array of values and splits the array into multiple | ||
| 693 | arrays where the maximum size of each array is as specified | ||
| 694 | Example : my $split_arrays = split_array($large_array, 10); | ||
| 695 | Returntype : ArrayRef of ArrayRefs where each element is a split list | ||
| 696 | =cut | ||
| 697 | |||
| 698 | sub split_array { | ||
| 699 | my ($amount, $array) = @_; | ||
| 700 | assert_ref($array, 'ARRAY', 'array'); | ||
| 701 | my @split; | ||
| 702 | my $counter = 0; | ||
| 703 | my $index = 0; | ||
| 704 | foreach my $e (@$array) { | ||
| 705 | if($counter == $amount) { | ||
| 706 | $index++; | ||
| 707 | $counter = 0; | ||
| 708 | } | ||
| 709 | push(@{$split[$index]}, $e); | ||
| 710 | $counter++; | ||
| 711 | } | ||
| 712 | return \@split; | ||
| 713 | } | ||
| 714 | |||
| 715 | =head2 scope_guard | ||
| 716 | |||
| 717 | Arg [1] : CodeRef The block of code to exit once it escapes out of scope | ||
| 718 | Description : Simple subroutine which blesses your given code reference into | ||
| 719 | a L<Bio::EnsEMBL::Utils::Scalar::ScopeGuard> object. This has | ||
| 720 | a DESTROY implemented which will cause the code reference | ||
| 721 | to execute once the object goes out of scope and its reference | ||
| 722 | count hits 0. | ||
| 723 | Returntype : Bio::EnsEMBL::Utils::Scalar::ScopeGuard | ||
| 724 | Example : my $v = 'wibble'; | ||
| 725 | { | ||
| 726 | #Build a guard to reset $v to wibble | ||
| 727 | my $guard = scope_guard(sub { $v = 'wibble'}); | ||
| 728 | $v = 'wobble'; | ||
| 729 | warn $v; | ||
| 730 | } | ||
| 731 | # $guard is out of scope; sub is triggered and $v is reset | ||
| 732 | warn $v; | ||
| 733 | Exceptions : Raised if argument was not a CodeRef | ||
| 734 | Status : Stable | ||
| 735 | |||
| 736 | =cut | ||
| 737 | |||
| 738 | sub scope_guard { | ||
| 739 | my ($callback) = @_; | ||
| 740 | assert_ref($callback, 'CODE', 'callback'); | ||
| 741 | return bless($callback, 'Bio::EnsEMBL::Utils::Scalar::ScopeGuard'); | ||
| 742 | } | ||
| 743 | |||
| 744 | 1; | ||
| 745 | |||
| 746 | #### SUPER SECRET PACKAGE. IGNORE ME | ||
| 747 | package Bio::EnsEMBL::Utils::Scalar::ScopeGuard; | ||
| 748 | $Bio::EnsEMBL::Utils::Scalar::ScopeGuard::VERSION = '114.0.0_50'; # TRIAL | ||
| 749 | $Bio::EnsEMBL::Utils::Scalar::ScopeGuard::VERSION = '114.0.050'; | ||
| 750 | sub DESTROY { | ||
| 751 | my ($self) = @_; | ||
| 752 | $self->(); | ||
| 753 | return; | ||
| 754 | } | ||
| 755 | |||
| 756 | 1; |