Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Perl

Idiom #94 Print the type of a variable

Print the name of the type of x. Explain if it is a static type or dynamic type.

This may not make sense in all languages.

print ref($x)||"SCALAR", "\n";

Example:
my $s = "foo";
my @a = (1,2);
my %h = {foo=>1, bar=>2};
my $o = bless {}, 'OBJECT';
print ref($_)||"SCALAR", "\n" for $s, \@a, \%h, $o;

Output:
SCALAR
ARRAY
HASH
OBJECT
(type x)

New implementation...
< >
programming-idioms.org