Talespin_Kit Talespin_Kit - 7 months ago 8
Perl Question

Perl ref() does not print the 'HASH' type

The following code does not print the 'HASH' type. What is wrong with this code ?

#! /usr/bin/perl
$prices{'pizza'} = 12.00;
$prices{'coke'} = 1.25;
$prices{'sandwich'} = 3.00;
print ref($prices);

Answer

First of all, you should put use strict; and use warnings; at the top of your script (and do that for all the future Perl code as well). After doing so, you will see the following:

Global symbol "%prices" requires explicit package name at ./a.pl line 4.
Global symbol "%prices" requires explicit package name at ./a.pl line 5.
Global symbol "%prices" requires explicit package name at ./a.pl line 6.
Global symbol "$prices" requires explicit package name at ./a.pl line 7.
Execution of ./a.pl aborted due to compilation errors.

What it means is that you tried to use to separate variables: a %prices hash and a $prices scalar.

After fixing variable declaration using my %prices;, you can get a reference to your %prices hash as follows:

my $prices_ref = \%prices;
print ref($prices_ref);