如何像在调用例程中一样取消引用子例程中的散列?

安迪 A。

我有一个 perl 例程,它从一个 .csv 文件中生成一个散列。应在子程序中检查这些值。

所以我有一个散列%my_values并调用子程序check (\%my_values)

sub read_csv {
    ...

    # key:   headline entry
    # value: the value in the current row
    %my_hash;

    ...
    my ($has_error, $err_msg) = check (\%my_hash);
}

sub check {
    my($hash_ref) = @_;
    %my_hash = %$hash_ref;

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

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

    return 0, "";
}

起初,这似乎工作正常。但后来我意识到,价格格式既没有改变,也没有关键article_number可用。

直接在参考文献上工作使它:


# 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_hash = %$hash_ref;复制引用而不是取消引用。

如何像在调用例程中一样取消引用子例程中的散列?

哈康海格兰

下面是使用所谓的新特征的示例refaliasing在Perl 5.22(任选地与所述组合引入declared_refs在5.26特征引入)

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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我可以像在 C++ 中一样在 python 中取消引用迭代器吗?

如何像子例程一样调用其他shell脚本?

在TinyMCE中,像在Gmail中一样,通过按Enter来中断引用

如何像在颤振中一样使用 tablelayout

如何像在 kivy 中一样进行支票?

像在Safari中一样检查Firefox中的元素?

像在 Java 中一样访问 PHP 中的对象

VBA EXCEL:如何在另一个子例程中调用一个子例程?

如何像在 Python 中一样在 C++ 中重新创建默认参数?

如何像在 Qtextedit 中一样在 Qtableview 中启用自动滚动?

如何像在 xml 中一样在 java 中设置 lottie 动画?

如何像在 Laravel 中一样在 Yii2 中制作动态属性?

如何像在 Windows 中一样在 ubuntu 中更改外部硬盘名称/驱动器

如何像在Eclipse中一样在Android Studio中复制错误

如何像在MATLAB中一样在Python中创建数字范围

如何像在 Windows 中一样在 Linux 中设置路径变量?

像在jQuery中一样如何在AgularJS中触发事件?

如何像在python中一样打印完整的整数,在c++中?

如何在Smarty中像在PHP中一样调试变量var_dump()

如何像在Vue中一样在Alpine.JS中添加显示过滤器?

如何像在autohotkey中一样在python中编程hotstrings

如何像在 CMD 中一样在 PowerShell 中执行启动命令?

如何在Swift中存储属性,就像在Objective-C中一样?

如何在功能组件中像在类组件中一样制作 onChange 跟踪功能

如何像在Objective-C中一样在Swift中定义块?

如何在 Word 中强调文本而不改变文档,就像在 Grammarly 中一样

如何像在Chrome中一样在Firefox中设置XHR断点

我如何像在Google Maps中一样在Android中获得连续的位置更新?

如何像在WebStorm中一样轻松地导航到VS Code中的界面实现?