[英]perl Not a HASH reference
Greeting community.问候社区。
This is related to Odd number of elements in hash assignment with default这与默认情况下哈希分配中元素的奇数有关
However, I use the reference as suggested but now I am passing a hash and a file handler and it gives this error Not a HASH reference when I call但是,我按照建议使用了该引用,但现在我正在传递一个哈希值和一个文件处理程序,并且在我调用时它给出了这个错误 Not a HASH 引用
hash2file2(\%m2,$rm3d_fh);
Heres my code.继承人我的代码。
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use POSIX qw(strftime);
#getDate return date in string format 20161010
sub getDate{
my $date = strftime "%Y%m%d", localtime;
#print $date;
return $date;
}
#end getDate
#
#file2hash : read the file in k<file_name> e.g.=kconfig & kmem into hash table
#
sub file2hash {
my ($file) = @_;
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
my %HoH;
my $key;
my $value;
my $who;
my $rec;
my $field;
#while ( my $line = <$data>) {
while ( <$data>) {
#print $line;
next unless (s/^(.*?):\s*//);
$who = $1;
#print $who;
$rec = {};
$HoH{$who} = $rec;
for $field ( split ) {
($key, $value) = split /=/, $field;
$rec->{$key} = $value;
}
}
return %HoH;
}
#
#end file2hash
#
#
#hash2print
#print out hash table in k<file_name> format
#
sub hash2print{
(my %HoH,my $debug) = @_;
#my ($debug)=@_||0;
#my %HoH = shift;
#my $debug = shift || 0;
my $family;
my $role;
for $family ( keys %HoH ) {
#print "$family\n";
for $role ( keys %{ $HoH{$family} } ) {
if ($debug){
print "family:$family\n";
print "role: $role\n";
}
print "$role=$HoH{$family}{$role}";
}
print "\n";
}
}
#
#end hash2print
#
#
#hash2file2
#print out hash table in k<file_name> format
#
sub hash2file2{
#(my %HoH,my $debug) = @_;
(my %HoH,my $fh) = @_;
#my %HoH = shift;
#my $fh = shift;
#my $debug = shift ||0;
my $family;
my $role;
for $family ( keys %HoH ) {
#print "$family\n";
for $role ( keys %{ $HoH{$family} } ) {
#if ($debug){
# print $fh "family:$family\n";
# print $fh "role: $role\n";
#}
print $fh "$role=$HoH{$family}{$role}";
}
print $fh "\n";
}
close $fh;
}
#
#end hash2file2
#
sub dispatch{
my $event= shift;
my $debug = shift||0;
my $mail_prog = shift || "mailx";
my $config_f = shift || "kconfig";
my $memory_f = shift || "kmem";
my %h2=&file2hash($config_f);
my %m2=file2hash($memory_f);
my $today=&getDate();
if ($debug){
print "$today\n";
print "$event\n";
print "$config_f\n";
print "$memory_f\n";
print "$mail_prog\n";
}
my $email1_tag="email1";
my $email1_cnt_tag="email1_cnt";
my $email2_tag="email2";
my $xemail1 = $h2{$event}{$email1_tag};
my $xemail2 = $h2{$event}{$email2_tag};
my $xemail1_cnt = $h2{$event}{$email1_cnt_tag};
#initialize today_event_cnt is not happened today
my $today_event_cnt = $m2{$today}{$event}||1;
if ($today_event_cnt == 1){
$m2{$today}{$event} = 1;
}
if ($debug){
print "$xemail1\n";
print "$xemail2\n";
print "$xemail1_cnt\n";
print "$today_event_cnt\n";
}
my $mail1_cmd_str = $mail_prog." -s ".$event." ".$xemail1;
my $mail2_cmd_str = $mail_prog." -s ".$event." ".$xemail2;
if ($today_event_cnt + 1 >$xemail1_cnt){
system "$mail2_cmd_str";
}else{
system "$mail1_cmd_str";
}
#hash2print(\%m2);
open my $rm3d_fh, '>', $memory_f or die "...$!";
hash2file2(\%m2,$rm3d_fh);
}#end dispatch
#my %h2=&file2hash("kconfig");
#my %m2=&file2hash("kmem");
#hash2print(%h2);
#hash2print(%m2);
#print &getDate();
#my $xcnt= &dispatch("event_c3_z2");
&dispatch("event_c3_z2",1,"echo");
#&dispatch("event_c3_z2",1);
#print $xcnt;
my test data file1我的测试数据文件 1
event_a1_x1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=8
event_a1_x2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=7
event_b2_y1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=6
event_b2_y2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=5
event_c3_z1: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=4
event_c3_z2: email1=ackee0000@gmail.com email2=kym018@gmail.com email1_cnt=3
test data file2测试数据文件2
20160926: event_a1_x1=7
20160926: event_a1_x2=6
20160926: event_b2_y1=5
20160926: event_b2_y2=4
20160926: event_c3_z1=3
20160926: event_c3_z2=2
I forget to de-reference我忘记取消引用
Odd number of elements in hash assignment with default 散列赋值中的奇数元素,默认情况下
sub hash2print {
(my $hashref, my $debug) = @_;
my %HoH = %$hashref; # dereference $hashref to get back the hash
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.