2016-04-23 48 views

回答

9

首先,你应该把use strict;use warnings;在脚本的顶部(并为所有未来的Perl代码以及)。这样做之后,你会看到以下内容:

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. 

它的意思是,你试图用分离变量:一个%prices哈希和$prices标。

固定使用my %prices;变量声明后,你可以得到一个关于你%prices哈希如下:

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

从正式的角度来看,答案可能更短:

  1. REF($价格)如果$ prices的值是对另一个变量的引用,则返回1,否则返回false。
  2. ref($ prices)是第一次使用未声明的变量$ prices(前面的行代表另一个未声明的变量 - hash%价格)。
  3. $ prices的价格是undef,ref($ prices)是一个空字符串。
  4. 也许,你的想法是写

    $prices->{'pizza'} = 12.00; $prices->{'coke'} = 1.25; $prices->{'sandwich'} = 3.00; print ref($prices);