Perl编译问题 [英] Perl compilation woes

查看:74
本文介绍了Perl编译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译此方法时遇到了麻烦:

I'm having some troble compiling this method:

#changes the names of the associations for $agentConf
#where the key value pairs in %associationsToChangeDict are the old and new values respectively
sub UpdateConfObjectAssociations{
    my($agentConf, %associationsToChangeDict) = @_;

    foreach my $association ($agentConf->GetAssociations()) {
        if ( grep {$_ eq $association->Name()} keys %associationsToChangeDict) {
            my $newValue = %associationsToChangeDict{$association->Name()};
            $association->Value($newValue);
        } 
    }   
}

这是错误消息:

syntax error at D:\Install\AutoDeployScripts\scripts\Perl/.\AI\SiteMinderHelper
.pm line 75, near "%associationsToChangeDict{"
syntax error at D:\Install\AutoDeployScripts\scripts\Perl/.\AI\SiteMinderHelper
.pm line 79, near "}"

任何人都可以看到问题出在哪里吗?

Can anyone see where the problem is?

推荐答案

是的,您可以像这样从哈希中获取一个切片(即多个值):

Yes, you can get a slice (i.e. multiple values) from a hash like this:

my @slice = @hash{ @select_keys };

您可以从如下所示的哈希中获取单个值:

And you can get a single value from a hash like this:

my $value = $hash{ $key };

但是您不能使用开头的'%'标记来解决哈希.缺少Perl 6(信号不会根据数字变化)是没有意义的.

But you cannot address a hash with a beginning '%' sigil. It's meaningless short of Perl 6 (where sigils won't change according to number).

由于您希望哈希中没有一个项目,因此您的分配应为:

Because you want a single item out of the hash, your assignment should be:

my $newValue = $associationsToChangeDict{ $association->Name() };

Perl中有三个上下文,分别是 void scalar list .标记比变量名的一部分更能表示上下文.当没有人期望表达式返回结果时,我们会看到一个 void 上下文.此上下文仅在sub -s中出现,这是程序员只想完成某件事,而不关心是否返回值.

There are three contexts in Perl, void, scalar, and list. A sigil is more an indicator of context than part of the variable name. We see a void context when nobody is expecting a result back from an expression. This context only occurs in sub-s, when the programmer just wants something done, and does not care if a value is returned.

在谈论变量时,只剩下 scalar list .这些工作就像语言中的单数形式和复数形式.由于Larry Wall在设计Perl时受到自然语言的影响,所以这些相似之处是很自然的.但是没有哈希上下文".当然,只是使问题复杂化了一点,当某种东西在标量上下文中包装时被评估为列表,也具有上下文的含义,它只是评估结果列表的大小.

That leaves only scalar and list when talking about variables. These kind of work like singular and plural forms in a language. Since Larry Wall was influenced by natural languages in designing Perl, these parallels are, well, natural. But there is no "hash context". Of course to complicate matters just slightly, something evaluated to be a list when wrapped in a scalar context, has a contextual meaning as well, it just evaluates to the magnitude of the resulting list.

您不太可能这样做(但它有其含义):

You would not likely do this (but it has a meaning):

my $count = @list[1..4];

但是您可以这样做:

my $count = ( grep { $_ % 2 == 0 } @list[ @subscripts ] );

它将在括号内进行所有列表上下文评估,以求出列表中项目总数的单个值. (尽管grep可能很聪明,可以计算成功次数,而不是形成一个新列表,因为上下文 是在Perl中传播的.)

It would do all that list context evaluation inside the parens, to evaluate to the single value of the total of items in the list. (Although grep is probably intelligent enough to count successes, instead of forming a new list since context is propagated in Perl.)

这篇关于Perl编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆