in reply to "Scalar found where operator expected", references

In addition to the missing comma, the following line is nonsense:
my %hash = $$hashref;
You can either make a complete copy of the hash by doing
my %hash = %$hashref;
or more efficiently, just delete that line and always directly use the hashref, eg
foreach my $key (keys %$hashref) { my $weight = $hashref->{$key}; ...

Dave.