Perl 如何在子例程中取消对散列的引用,并像在调用例程中一样对其进行处理?

Perl 如何在子例程中取消对散列的引用,并像在调用例程中一样对其进行处理?,perl,hash,reference,subroutine,Perl,Hash,Reference,Subroutine,我有一个perl例程,它对.csv文件进行哈希处理。应在子例程中检查这些值 因此,我有一个散列%my\u值,并调用子例程检查(\%my\u值): 起初,这似乎效果不错。但后来我意识到,无论是价格格式还是商品编号都没有改变 直接在参考资料上工作使其: # In this case, it works! sub check { my($hash_ref) = @_; # Get the artikel number of the article $hash_ref->

我有一个perl例程,它对.csv文件进行哈希处理。应在子例程中检查这些值

因此,我有一个散列
%my\u值
,并调用子例程
检查(\%my\u值)

起初,这似乎效果不错。但后来我意识到,无论是价格格式还是商品编号都没有改变

直接在参考资料上工作使其:


# In this case, it works!
sub check {
    my($hash_ref) = @_;

    # Get the artikel number of the article
    $hash_ref->{'article_number'} = get_artnr($hash_ref->{'article'});
    if (not $hash_ref->{'article_number'}) {
        return 1, "Article $hash_ref->{'article'} not found!";
    }

    # check price (I'm in germany, there must be a conversation from ',' to '.')
    $hash_ref->{'price'} =~ s/,/./;
    if (not $hash_ref->{'price'} =~ m/^\d+(\.\d+)?$/) {
        return 1, "Invalid format of price";
    }

    return 0, "";
}
所以我认为
%my\u hash=%$hash\u ref复制引用,而不是取消引用


如何在子例程中取消对散列的引用,并像在调用例程中一样对其进行处理?

下面是一个使用Perl 5.22中引入的新功能(可选地与5.26中引入的功能组合)的示例

或者,您可以使用:


使用或箭头运算符
$hash_ref->{key}
,有关详细信息,请参阅information@HåkonHæ腺体哦,再磨蚀被标记为实验性的。箭头操作符很好。但是我不能取消对散列的引用吗(比如在
c
中)?
使用featureqw(声明的\u refs重构);没有“实验性::重构”警告;没有“实验性::声明的”警告=>
使用实验性qw(重新调整声明的参考)请注意,虽然这些功能是实验性的,但我怀疑它们会被接受。@ikegami“使用实验性…”是的,我尝试过,但仍然收到警告
声明引用是实验性的
通过引用进行别名是实验性的
。不知道会有什么问题。@ikegami我想我现在明白了原因,我需要在
使用实验性
之前使用警告
,否则警告将重新启用,因为“我想我现在明白了原因”,是的,这真的相当于
使用功能qw(声明的\u refs重构);没有“实验性::重构”警告;没有“实验性::声明的”警告。如果以后重新启用警告,那么。。。

# In this case, it works!
sub check {
    my($hash_ref) = @_;

    # Get the artikel number of the article
    $hash_ref->{'article_number'} = get_artnr($hash_ref->{'article'});
    if (not $hash_ref->{'article_number'}) {
        return 1, "Article $hash_ref->{'article'} not found!";
    }

    # check price (I'm in germany, there must be a conversation from ',' to '.')
    $hash_ref->{'price'} =~ s/,/./;
    if (not $hash_ref->{'price'} =~ m/^\d+(\.\d+)?$/) {
        return 1, "Invalid format of price";
    }

    return 0, "";
}
use v5.26;
use warnings;  # IMPORTANT: this line must come before "use experimental"
use strict;
use feature qw(say); 
use experimental qw(declared_refs refaliasing);

{  # <-- introduce scope to avoid leaking lexical variables into subs below
    my %hash = (a=>1, b=>2);
    check(\%hash);
    say "Value of 'a' key is now: ", $hash{a};
}

sub check {
    my (\%hash) = @_;

    $hash{a} = 3;
}
Value of 'a' key is now: 3
sub check {
    my ($hash) = @_;

    $hash->{a} = 3;
}