B
bill
I want to write a sub that takes either a string which is interpreted
as a filename to be opened and read, or an open read handle. The
question is how to test for the latter? At first I thought I'd
just test
ref $_[0] eq 'GLOB';
but then I realized that $_[0] could be an object of type IO::Handle,
or something like it. And besides, even if $in is a variable of
type GLOB, this does not ensure that <$in> is a valid operation.
After convincing myself that testing for GLOBness was *not* the
thing to do, I thought I could test
UNIVERSAL::can($_[0], 'getline');
but I soon discovered that if $in is defined by
open (my $in, 'foobar') or die "Can't read foobar: $!\n";
then UNIVERSAL::can($my, 'getline') fails.
By this point I get the distinct feeling that I just don't know
enough about this wheel to mess around with reinventing it. What's
the tried-and-true idiom for doing what I want to do?
Thanks!
bill
as a filename to be opened and read, or an open read handle. The
question is how to test for the latter? At first I thought I'd
just test
ref $_[0] eq 'GLOB';
but then I realized that $_[0] could be an object of type IO::Handle,
or something like it. And besides, even if $in is a variable of
type GLOB, this does not ensure that <$in> is a valid operation.
After convincing myself that testing for GLOBness was *not* the
thing to do, I thought I could test
UNIVERSAL::can($_[0], 'getline');
but I soon discovered that if $in is defined by
open (my $in, 'foobar') or die "Can't read foobar: $!\n";
then UNIVERSAL::can($my, 'getline') fails.
By this point I get the distinct feeling that I just don't know
enough about this wheel to mess around with reinventing it. What's
the tried-and-true idiom for doing what I want to do?
Thanks!
bill