#!/usr/bin/perl use strict; use warnings; # Ivan Novick # ref_test.pl # use the ref function to determine if a variable contains a reference or someother type sub testit { my $arg = shift; my $description = shift; # ref will evaluate to false if $arg is not a reference if (ref($arg)) { # ref will print out the type that the reference is to print ref($arg) . " --- $description\n"; } else { print "NULL --- $description\n"; } } my $myscalar = "aa"; my @myarray = qw/one two three/; my %myhash = qw/key1 value1 key2 value2/; my $ref1 = \$myscalar; my $ref2 = \@myarray; my $ref3 = \%myhash; # ref returns NULL testit("HELLO", "literal"); # ref returns NULL testit($myscalar, "scalar"); # ref returns NULL testit(@myarray, "array"); # ref returns NULL testit(%myhash, "hash"); # ref returns SCALAR testit($ref1, "reference to a scalar"); # ref returns ARRAY testit($ref2, "reference to an array "); # ref returns HASH testit($ref3, "reference to a hash");