The documentation for ref() mentions several possible return values. I understand most of them, but not REF
, IO
, and LVALUE
. How would I write Perl code to cause ref
to return those values?
After reading the documentation on typeglobs and file handles, I came close for IO
with this code:
open(INPUT, '<', 'foo.pl');
print ref(*INPUT{IO}), "\n"; # Prints IO::Handle
For REF
and LVALUE
I tried several bizarre constructions, but had no success.
Here's a quick and easy way to produce most of them:
use 5.010;
say 'SCALAR: ', ref \undef;
say 'ARRAY: ', ref [1..5];
say 'HASH: ', ref { key => 'value' };
say 'CODE: ', ref sub {};
say 'REF: ', ref \\undef;
say 'GLOB: ', ref \*_;
say 'LVALUE: ', ref \substr "abc", 1, 2;
say 'LVALUE: ', ref \vec 42, 1, 2;
say 'FORMAT: ', ref *STDOUT{FORMAT}; # needs declaration below
say 'IO: ', ref *STDIN{IO}; # actually prints IO::Handle
say 'VSTRING: ', ref \v5.10.0;
say 'Regexp: ', ref qr/./;
format =
.
REF is just a reference to another reference. LVALUE is a special case of a scalar that has an external influence if it is modified.
IO is the base type behind the handles, you can make it appear explicitely using Acme::Damn from CPAN. As noted by Michael Carman in the comments, you really shouldn't be unblessing objects — don't use in real code.
use Acme::Damn;
say 'IO: ', ref damn *STDIN{IO}; # really prints IO
The source for the ref
function also has bits of code to display "BIND" and "UNKNOWN", but there shouldn't be a way to get those without messing with the internals. Blead also has an interesting unblessed "REGEXP" (different from the "Regexp" above); if someone knows how to make ref
yield that...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With