【问题标题】:Using a Hash as a reference is deprecated - Perl不推荐使用哈希作为参考 - Perl
【发布时间】:2012-11-14 17:45:10
【问题描述】:

我正在尝试在我的 Windows 平台上安装 scmbug。 我在以下方法中将哈希作为参考错误。查看“HERE”注释以了解它发生的位置。

我应该用 $mail 替换每个 %mail 吗?

我试过了,但出现了其他错误。话说

全局符号“$mail”需要明确的包名

如何解决这个问题?

sub mail_notification_send_mail {
   my $self = shift;
   my ($request, $subject, $message, $mailing_list) = ( @_ );
   my %mail;

   # Set values. This permits users to override the default behavior
   # of Mail::Sendmail
   %mail = \%{ $request->{ policies }->{ mail_notification }->{ mail_settings } };
   %mail->{ 'X-Mailer' } = "Mail::Sendmail version $Mail::Sendmail::VERSION";  HERE
   %mail->{ Subject } = $subject; HERE
   %mail->{ Message } = $message; HERE

   #
   # Since we just reset %mail to values, we haven't really picked up
   # the To,From,Reply-To that were computed. We do this next
   #

   # Don't blindly ignore the To list that was computed
   my $mailing_list_to = $self->emails_to_csv( $mailing_list->{ To } );
   %mail->{ To } = $mailing_list_to; HERE

   # Don't blindly ignore the From list that was computed
   my $mailing_list_from = $self->emails_to_csv( $mailing_list->{ From } );
   %mail->{ From } = $mailing_list_from; HERE

   # Don't blindly ignore the 'Reply-To' list that was computed
   my $mailing_list_replyto = $self->emails_to_csv( $mailing_list->{ 'Reply-To' } );
   %mail->{ 'Reply-To' } = $mailing_list_replyto; HERE


   if ( !sendmail(%mail) ) {
   my $msg = "Sending mail on " . $request->{ name } .
       " failed. Log error was:\n" . $Mail::Sendmail::error . "\n";
   $request->add_result(1, $msg);
   }
}

谢谢

【问题讨论】:

    标签: perl hash reference deprecated


    【解决方案1】:

    文档:perldoc perlref

    你有一个 hashref,但你正在使用一个 hash 的印记。将所有%mail 替换为$mail

    你有:

    %mail = \%{ $request->{ policies }->{ mail_notification }->{ mail_settings } };
    

    \% 告诉 perl 取消对哈希的引用并返回一个 hashref,但您将它分配给一个哈希。我敢打赌你也会在那里收到警告。该行应该是:

    $mail = $request->{ policies }->{ mail_notification }->{ mail_settings }; ## change all `%mail` to `$mail`
    

    %mail = %{ $request->{ policies }->{ mail_notification }->{ mail_settings } }; ## change all `%mail->{something}` to `$mail{something}`. 
    

    【讨论】:

    • 我认为你的第一行应该是我的 %hash
    • @Disco3,我已经对其进行了编辑并完全改变了我的答案。我没有注意到他正在将 hashref 分配给哈希。还是谢谢你。
    【解决方案2】:

    使用$mail->{ ... } 以取消引用。但是在您的情况下,您有一个哈希 %mail 而不是引用,因此访问其成员的正确方法是没有取消引用运算符 ->。一个简单的$mail{...} 就足够了。

    【讨论】:

    • %mail 哈希被分配了一个 hashref,因此 OP 需要做更多的工作,而不仅仅是改变它的访问方式。
    • 是的。删除 ` in %mail = \%{...` 就足够了,就像接受的答案所述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 2017-11-24
    • 2018-01-07
    • 2011-03-16
    • 2021-05-09
    • 2018-12-07
    相关资源
    最近更新 更多