本文介绍了我如何与具有哈希属性的Perl对象进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 sub new { my($ class,$ name)= @_; my $ self = { _name => $ name, ... _runs => (), _times => [], ... }; bless($ self,$ class); 返回$ self; } 现在,我正在尝试创建一个访问器/以及将新数据推入散列的另一个子例程。但我有一段时间让所有引用/解引用/ $自调用一起工作。我已经烧了我的眼睛与不能使用字符串(blah)作为HASH裁判等等错误。 对于访问器,什么是返回哈希的'最佳实践'?我应该使用哪一个选项(如果有)?: return $ self-> {_ runs}; return%{$ self-> {_ runs}}; return \ $ self-> {_ runs}; 另外,当我在类中的其他子例程中使用散列时,我使用什么语法复制它? my @runs = $ self-> {_ runs}; my @runs =%{$ self-> {_ runs}}; my @runs = $%{$ self> {_ runs}}; my @runs = $$ self-> {_ runs}; 同样可以遍历键: foreach my $ dt(keys%{$ self-> {_ runs}}) 实际添加数据如何? $ self> {_ runs} {$ dt} = $ duration; %{$ self> {_ runs}} {$ dt} = $ duration; $$自> {_运行} {$ dt} = $持续时间; 你明白了。我一直在阅读关于使用类的文章,以及关于引用和解引用的文章,但我似乎无法让我的大脑同时结合知识和使用两者。我最后得到了我的_times数组,但是将我的数组语法转换为哈希并不起作用。 在对象中进行数组或哈希运算。要将它们用于标准函数,您需要对它们进行解引用。例如: @ {$ self-> {_ array_ref_key}}; %{$ self->> {_ hash_ref_key}}; 如果您需要传递参数给标准函数: push(@ {$ self-> {_ array_ref_key}},$ some_value); 为我的$ hash_key(键%{$自我> {_ hash_ref_key}}){ $自> {_ hash_ref_key} {$ hash_key}; ##你可以通过引用} 来访问散列值。 $ self_gt; {_ hash_ref_key} {$ hash_key} 语法是 $ self-> {_ hash_ref_key} - > {$ hash_key} (如果你第一次看到它,它可能有意义)。 另请参阅相应的手册页。 I have a class with several variables, one of which is a hash (_runs):sub new{ my ($class, $name) = @_; my $self = { _name => $name, ... _runs => (), _times => [], ... }; bless ($self, $class); return $self;}Now, all I'm trying to do is create an accessor/mutator, as well as another subroutine that pushes new data into the hash. But I'm having a hell of a time getting all the referencing/dereferencing/$self calls working together. I've about burned my eyes out with "Can't use string ("blah") as a HASH ref etc etc" errors.For the accessor, what is 'best practice' for returning hashes? Which one of these options should I be using (if any)?:return $self->{_runs};return %{ $self->{_runs} };return \$self->{_runs};Further, when I'm using the hash within other subroutines in the class, what syntax do I use to copy it?my @runs = $self->{_runs};my @runs = %{ $self->{_runs} };my @runs = $%{ $self->{_runs} };my @runs = $$self->{_runs};Same goes for iterating over the keys:foreach my $dt (keys $self->{_runs})foreach my $dt (keys %{ $self->{_runs} })And how about actually adding the data?$self->{_runs}{$dt} = $duration;%{ $self->{_runs} }{$dt} = $duration;$$self->{_runs}{$dt} = $duration;You get the point. I've been reading articles about using classes, and articles about referencing and dereferencing, but I can't seem to get my brain to combine the knowledge and use both at the same time. I got my _times array working finally, but mimicking my array syntax over to hashes didn't work. 解决方案 You are storing references to array or hashes in your object. To use them with standard functions you'll need to dereference them. For example:@{ $self->{_array_ref_key} }; %{ $self->{_hash_ref_key} };If you need pass parameters to standard function:push( @{ $self->{_array_ref_key} }, $some_value );for my $hash_key ( keys %{ $self->{_hash_ref_key} }) { $self->{_hash_ref_key}{$hash_key}; ## you can access hash value by reference}Also $self->{_hash_ref_key}{$hash_key} syntax is shortcut for $self->{_hash_ref_key}->{$hash_key} (which can make for sense if you see it first time).Also take a look at corresponding manual page. 这篇关于我如何与具有哈希属性的Perl对象进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 03:05