作成したクラスの等価 (==) 演算子をオーバーライドしようとしていますが、現在問題に直面しており、解決方法がわかりません。どんな助けでも大歓迎です。
クラスのnew()
サブは次のとおりです。
sub new
{
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {@_};
bless($self, $class);
return $self;
}
等値演算子のオーバーロードは次のとおりです。
use overload ('==' => \&compare);
sub compare
{
my ($lhs, $rhs, $swap) = @_;
my $lhsSize = keys(%{$lhs});
my $rhsSize = keys(%{$rhs});
if($lhsSize != $rhsSize) { return 0; } # If objects don't have the same number of fields, they cannot be identical
while (my ($lhsKey, $lhsValue) = each(%{$lhs})) # Loop through the fields
{
my $rhsValue = %{$rhs}->{$lhsKey};
print("Key: $lhsKey Comparing $lhsValue with $rhsValue");
if($rhsValue ne $lhsValue)
{
return 0;
}
}
return 1;
}
Using a hash as a reference is deprecated at Cashflow.pm line 43.
ここで、 43 行目のエラーが表示されますmy $rhsValue = %{$rhs}->{$lhsKey};
。次に、解決策はを削除することを示唆するこのスレッドを見つけました->
が、行を変更するmy $rhsValue = %{$rhs}{$lhsKey};
と構文エラーが発生します。
お分かりかもしれませんが、私は Perl の専門家ではありませんが、なぜこれが機能しないのかわかりません。
助けてくれてありがとう。
マーク