#!/usr/bin/env perl -l use strict; use warnings; use IO::Handle; my $complex_data_hash_ref = { not_used_1 => '', example => { undefined => undef, empty_string => '', non_empty_string => 'string', empty_array_ref => [], non_empty_array_ref => [ 1, 2, 3 ], empty_hash_ref => {}, non_empty_hash_ref => { x => 24, y => 25, z => 26 }, scalar_ref => \42, glob_ref => \*STDOUT, code_ref => sub { print "\n\t\tHello, world!\n" }, ref_ref => \\99, blessed_ref => IO::Handle::->new(), ref_ref_ref => \\\999, regex => qr{match}, vstring => v65, vstring_ref => \v65, }, not_used_2 => '', }; my @example_keys_to_parse = qw{ undefined empty_string non_empty_string empty_array_ref non_empty_array_ref empty_hash_ref non_empty_hash_ref glob_ref code_ref ref_ref blessed_ref ref_ref_ref regex vstring vstring_ref }; for my $example_key (@example_keys_to_parse) { my $example_key_value = $complex_data_hash_ref->{example}{$example_key}; examine_data($example_key, $example_key_value); } sub examine_data { my ($in_key, $in_value) = @_; print "Examining '$in_key':"; if (ref $in_value) { print "\tThe value of '$in_key' is a reference."; print "\tThe value of '$in_key' is '$in_value'."; my $ref = ref $in_value; print "\tThe ref() value of '$in_key' is '$ref'."; if ($ref eq 'SCALAR') { my $deref = ${$in_value}; print "\tIts dereferenced value is '$deref'."; } elsif ($ref eq 'ARRAY') { my @deref = @{$in_value}; if (@deref) { print "\tIts dereferenced value has these elements: ", "'@deref'."; } else { print "\tIts dereferenced value has no elements."; } } elsif ($ref eq 'HASH') { my %deref = %{$in_value}; my @deref_keys = keys %deref; if (@deref_keys) { print "\tIts dereferenced value has these keys: ", "'@deref_keys'."; my @deref_values = values %deref; print "\tIts dereferenced value has these values: ", "'@deref_values'."; } else { print "\tIts dereferenced value has no key-value pairs."; } } elsif ($ref eq 'GLOB') { print $in_value "\tA globref can be a filehandle opened for writing."; } elsif ($ref eq 'CODE') { $in_value->(); } elsif ($ref eq 'REF') { examine_data('dereferenced__' . $in_key, ${$in_value}); } else { print "\tTODO: Further work on this type ", 'is left as an exercise for the reader.'; } } else { print "\tThe value of '$in_key' is not a reference."; if (defined $in_value) { print "\tThe value of '$in_key' is defined."; if (length $in_value) { print "\tThe value of '$in_key' is '$in_value'."; } else { print "\tThe value of '$in_key' is zero-length."; } } else { print "\tThe value of '$in_key' is not defined."; } } } #### Examining 'undefined': The value of 'undefined' is not a reference. The value of 'undefined' is not defined. Examining 'empty_string': The value of 'empty_string' is not a reference. The value of 'empty_string' is defined. The value of 'empty_string' is zero-length. Examining 'non_empty_string': The value of 'non_empty_string' is not a reference. The value of 'non_empty_string' is defined. The value of 'non_empty_string' is 'string'. Examining 'empty_array_ref': The value of 'empty_array_ref' is a reference. The value of 'empty_array_ref' is 'ARRAY(0x7fdcfe8040b0)'. The ref() value of 'empty_array_ref' is 'ARRAY'. Its dereferenced value has no elements. Examining 'non_empty_array_ref': The value of 'non_empty_array_ref' is a reference. The value of 'non_empty_array_ref' is 'ARRAY(0x7fdcfe82bf58)'. The ref() value of 'non_empty_array_ref' is 'ARRAY'. Its dereferenced value has these elements: '1 2 3'. Examining 'empty_hash_ref': The value of 'empty_hash_ref' is a reference. The value of 'empty_hash_ref' is 'HASH(0x7fdcfe82c048)'. The ref() value of 'empty_hash_ref' is 'HASH'. Its dereferenced value has no key-value pairs. Examining 'non_empty_hash_ref': The value of 'non_empty_hash_ref' is a reference. The value of 'non_empty_hash_ref' is 'HASH(0x7fdcfe82c0f0)'. The ref() value of 'non_empty_hash_ref' is 'HASH'. Its dereferenced value has these keys: 'y z x'. Its dereferenced value has these values: '25 26 24'. Examining 'glob_ref': The value of 'glob_ref' is a reference. The value of 'glob_ref' is 'GLOB(0x7fdcfe82c2d0)'. The ref() value of 'glob_ref' is 'GLOB'. A globref can be a filehandle opened for writing. Examining 'code_ref': The value of 'code_ref' is a reference. The value of 'code_ref' is 'CODE(0x7fdcff08c5a0)'. The ref() value of 'code_ref' is 'CODE'. Hello, world! Examining 'ref_ref': The value of 'ref_ref' is a reference. The value of 'ref_ref' is 'REF(0x7fdcfe82c1b0)'. The ref() value of 'ref_ref' is 'REF'. Examining 'dereferenced__ref_ref': The value of 'dereferenced__ref_ref' is a reference. The value of 'dereferenced__ref_ref' is 'SCALAR(0x7fdcff07b598)'. The ref() value of 'dereferenced__ref_ref' is 'SCALAR'. Its dereferenced value is '99'. Examining 'blessed_ref': The value of 'blessed_ref' is a reference. The value of 'blessed_ref' is 'IO::Handle=GLOB(0x7fdcfe82c240)'. The ref() value of 'blessed_ref' is 'IO::Handle'. TODO: Further work on this type is left as an exercise for the reader. Examining 'ref_ref_ref': The value of 'ref_ref_ref' is a reference. The value of 'ref_ref_ref' is 'REF(0x7fdcfe82c480)'. The ref() value of 'ref_ref_ref' is 'REF'. Examining 'dereferenced__ref_ref_ref': The value of 'dereferenced__ref_ref_ref' is a reference. The value of 'dereferenced__ref_ref_ref' is 'REF(0x7fdcfe84b2d0)'. The ref() value of 'dereferenced__ref_ref_ref' is 'REF'. Examining 'dereferenced__dereferenced__ref_ref_ref': The value of 'dereferenced__dereferenced__ref_ref_ref' is a reference. The value of 'dereferenced__dereferenced__ref_ref_ref' is 'SCALAR(0x7fdcfe8441c8)'. The ref() value of 'dereferenced__dereferenced__ref_ref_ref' is 'SCALAR'. Its dereferenced value is '999'. Examining 'regex': The value of 'regex' is a reference. The value of 'regex' is '(?^:match)'. The ref() value of 'regex' is 'Regexp'. TODO: Further work on this type is left as an exercise for the reader. Examining 'vstring': The value of 'vstring' is not a reference. The value of 'vstring' is defined. The value of 'vstring' is 'A'. Examining 'vstring_ref': The value of 'vstring_ref' is a reference. The value of 'vstring_ref' is 'VSTRING(0x7fdcff02e130)'. The ref() value of 'vstring_ref' is 'VSTRING'. TODO: Further work on this type is left as an exercise for the reader.