When does ref($variable) return 'IO'?
- by Zaid
Here's the relevant excerpt from the documentation of the ref function:
The value returned depends on the type of thing the reference is a reference to. Builtin types include:
SCALAR
ARRAY
HASH
CODE
REF
GLOB
LVALUE
FORMAT
IO
VSTRING
Regexp
Based on this, I imagined that calling ref on a filehandle would return 'IO'. Surprisingly, it doesn't:
use strict;
use warnings;
open my $fileHandle, '<', 'aValidFile';
close $fileHandle;
print ref $fileHandle; # prints 'GLOB', not 'IO'
perlref tries to explain why:
It isn't possible to create a true
reference to an IO handle (filehandle
or dirhandle) using the backslash
operator. The most you can get is a
reference to a typeglob, which is
actually a complete symbol table
entry [...] However, you can still
use type globs and globrefs
as though they were IO handles.
In what circumstances would ref return 'IO' then?