0

我有一个通过 Data::Dumper - Dumper($cat_response->result->{'categories'}) 显示的响应

$VAR1 = { 'literature' => '1120', 'law' => '1153', 'arts and crafts' => '1132', 'children' => '1141', 'hobbies' => '1133', 'economy' => '1166', 'jobs' => '1140', 'media' => '1144', 'music' => '1147', 'animals' => '1170', 'business' => '1119', 'diet' => '1122', 'travel reviews' => '1154', 'jewelry' => '1157', 'movies' => '1146', 'television' => '1125', 'politics' => '1168', 'internet' => '1139', 'history' => '1129', 'recipes' => '1156', 'press releases' => '1151', 'presents' => '1128', 'marketing' => '1143', 'translations' => '1162', 'fashion' => '1145', 'technology' => '1163', 'real estate' => '1138', 'computer' => '1173', 'automobile' => '1116', 'finances' => '1126', 'weddings' => '1134', 'games' => '1127', 'esoterism' => '1124', 'horoscopes' => '1135', 'shopping' => '1123', 'humor' => '1137', 'miscellaneous' => '1159', 'science' => '1167', 'programming' => '1152', 'languages' => '1161', 'beauty' => '1117', 'sports' => '1160', 'hotels' => '1136', 'plants' => '1149', 'education' => '1118', 'traveling' => '1155', 'health' => '1130', 'telecommunication' => '1164', 'environment' => '1171', 'software' => '1158', 'sweepstakes' => '1131', 'logistics' => '1142', 'home and family' => '1169', 'news' => '1148' };

要访问它,我使用:

my %categories = $cat_response->result->{'categories'};
foreach my $cat (keys (%categories)) {
    <option value="<% $categories{'$cat'} %>"><% $cat %></option>
}

但是,Dumper($cat) 的值是: $VAR1 = 'HASH(0x7fe972641560)';

我错过了什么?

4

1 回答 1

2

你缺use strict; use warnings;一个。(好吧,要么这样,要么你忘了告诉我们 Perl 告诉过你你的问题。)

$cat_response->result->{'categories'}包含对哈希的引用。将其分配给哈希是没有意义的。

my $categories = $cat_response->result->{'categories'};
foreach my $cat (keys (%$categories)) {
    <option value="<% $categories->{'$cat'} %>"><% $cat %></option>
}
于 2012-10-17T20:46:15.360 回答