Commit b2fd2412 authored by Andy Yates's avatar Andy Yates
Browse files

Adding a code for asserting if a reference is a filehandle or not

parent 4b4ab262
......@@ -92,18 +92,18 @@ our @EXPORT_OK;
@EXPORT_OK = qw(
check_ref check_ref_can
assert_ref assert_ref_can assert_numeric assert_integer assert_boolean assert_strand
assert_ref assert_ref_can assert_numeric assert_integer assert_boolean assert_strand assert_file_handle
wrap_array
);
%EXPORT_TAGS = (
assert => [qw(assert_ref assert_ref_can assert_integer assert_numeric assert_boolean assert_strand)],
assert => [qw(assert_ref assert_ref_can assert_integer assert_numeric assert_boolean assert_strand assert_file_handle)],
check => [qw(check_ref check_ref_can)],
array => [qw/wrap_array/],
all => [@EXPORT_OK]
);
use Bio::EnsEMBL::Utils::Exception qw(throw);
use Scalar::Util qw(blessed looks_like_number);
use Scalar::Util qw(blessed looks_like_number openhandle);
=head2 check_ref()
......@@ -367,4 +367,41 @@ sub assert_strand {
}
=head2 assert_file_handle
Arg [1] : The Scalar to check
Arg [2] : The attribute name you are asserting; not required but allows
for more useful error messages to be generated. Defaults to
C<-Unknown->.
Description : A subroutine which checks to see if the given scalar is
actually a file handle. This will handle those which are Glob
references and those which inherit from C<IO::Handle>. It will
also cope with a blessed Glob reference.
Returntype : Boolean;
Example : assert_file_handle($fh, '-FILE_HANDLE');
Exceptions : Raised if not defined, not a reference and was not a
GLOB or did not inherit from IO::Handle
Status : Stable
=cut
sub assert_file_handle {
my ($file_handle, $attribute_name) = @_;
$attribute_name ||= '-Unknown-';
throw "Attribute $attribute_name was undefined" if ! defined $file_handle;
my $ref = ref($file_handle);
throw "Attribute $attribute_name was not a reference. Got: $file_handle" if ! $ref;
if(!openhandle($file_handle)) {
if(blessed($file_handle)) {
if(! $file_handle->isa('IO::Handle')) {
throw "Attribute $attribute_name was blessed but did not inherit from IO::Handle. Ref was: $ref";
}
}
else {
throw "Attribute $attribute_name was not a file handle. Ref was: $ref";
}
}
return 1;
}
1;
......@@ -6,6 +6,7 @@ use Test::Exception;
use Bio::EnsEMBL::Utils::Scalar qw(:all);
use Bio::EnsEMBL::IdMapping::TinyGene;
use IO::Handle;
my $gene = Bio::EnsEMBL::IdMapping::TinyGene->new_fast([]);
......@@ -111,4 +112,24 @@ dies_ok { assert_boolean(-1) } 'Passing in integer -1 means death';
lives_ok { assert_strand(1) } 'Passing in integer 1 means lives';
lives_ok { assert_strand(0) } 'Passing in integer 0 means lives';
#File handles
my $scalar;
my $other_scalar;
open my $scalar_fh, '>', \$scalar;
open my $other_scalar_fh, '>', \$other_scalar;
bless($other_scalar_fh);
my $io_handle = IO::Handle->new(); # no need to close as it isn't opened yet just created
throws_ok { assert_file_handle(undef) } qr/undefined/, 'Passing in undefined scalar means death';
dies_ok { assert_file_handle(bless(1, 'Brian'), 'met')} 'Passing in a blessed scalar means death';
dies_ok { assert_file_handle('hello')} 'Passing in a String scalar means death';
dies_ok { assert_file_handle({})} 'Passing in a HashRef means death';
dies_ok { assert_file_handle(1E-10) } 'Passing in scientific notation numeric means death';
dies_ok { assert_file_handle(1.2) } 'Passing in floating point means death';
dies_ok { assert_file_handle(-1) } 'Passing in integer -1 means death';
dies_ok { assert_file_handle(1) } 'Passing in integer 1 means death';
lives_ok { assert_file_handle($scalar_fh) } 'Passing in a scalar FH means lives';
lives_ok { assert_file_handle($other_scalar_fh) } 'Passing in a blessed scalar FH means lives';
lives_ok { assert_file_handle($io_handle) } 'Passing in an IO::Handle means lives';
close($_) for ($scalar_fh, $other_scalar_fh);
done_testing();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment