When does ref($variable) return 'IO'?

Posted by Zaid on Stack Overflow See other posts from Stack Overflow or by Zaid
Published on 2010-06-02T06:20:26Z Indexed on 2010/06/02 6:23 UTC
Read the original article Hit count: 276

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about perl

Related posts about filehandle