你如何取消引用返回值?

qod*_*nja 2 oop perl return-value dereference

我一直遇到解除引用的问题,特别是在从函数返回值时.

问题似乎是每当你返回标量以外的任何东西时,你实际上是通过引用返回该对象 - 这对我来说没问题 - 但是当我们将这些引用传递给其他函数并且我们需要再次访问它们的内容时,我们怎么做做得对吗?

我一直遇到这样的错误:"期望偶数个参数得到参考"或其他类似的效果.

是否有一般的经验法则可以用来简化整个过程?我几乎希望我不必担心解除引用!

这是我今天早些时候尝试做的事情的一个例子,并遇到了各种各样的解引用问题,我花了几个小时试图抨击我的方式 - 所以在阅读,尝试和失败后,我在这里问你对于低迷.

人物

Person
 has name [Str]
 has madeby [Str]
 has height [Num]
 has id [Num]
Run Code Online (Sandbox Code Playgroud)

制作Person对象的各种方法

sub person_maker{
 my($this,%options) = @_;
 my $person = Person->new(%options);
   return $person;
}

sub make_person_named_james{
 my($this,$options) = @_;
 my $default = { name => 'James', madeby => 'name' };
   $options = ($options,$defaults); #merge default and options hash
   return($this->person_maker($options));
}

sub make_person_from_id{
 my($this,$id) = @_;
 my $default = { name => 'nameless person', madeby => 'id' };
   $default = ($default,{ id => $id });
   return($this->person_maker($default);
}

sub make_person_from_person{
 my($this,$person) = @_;
 my $person_options = {
   name => $person->name().'_twin',
   height => $person->height(),
   id => $person->id() + 1,
   madeby => 'person'
 };
 return($this->person_make($person_options));
}
Run Code Online (Sandbox Code Playgroud)
  • Func返回hash object =>它实际上返回哈希引用
  • Func返回哈希引用 =>它实际上以标量形式返回
  • Func返回array object =>它实际上返回一个数组引用
  • Func返回数组reference =>它实际上返回一个标量
  • Func返回一个标量 =>它实际上返回标量的值?

如果我理解了任何错误,请纠正我.

那么另一个问题就是消耗函数的参数.

根据我的回报,这些坏男孩的行为都会有所不同!

    sub myfunc{
      my($self,$args) = @_ # list of arguments (what if the args are mixed?)
  OR
      my($self,$args) = $_ # scalar (this wont work here $args will by undef
  OR
      my $self = shift; # pop the first arg
      my $args = shift; # pop the second arg
  OR
      my $self = $_[0] 
      my $args = $_[1]
Run Code Online (Sandbox Code Playgroud)

加!有太多的文件,其中许多已经过时,所以在这些情况下很难弄清楚正确或最好的事情.

如果有人有一个魔术图表说明何时使用这些不同的设置,以及如何在某些情况下取消引用,祝福哈希,哈希参考,标量等等我会永远感激,因为我浪费了几个小时试图破译这个.

Ala*_*avi 10

所有引用都是标量值

取消引用引用要求您知道引用的类型.可以使用该ref函数找到引用的类型.

my $array_ref = [];
print ref $array_ref;  # prints: ARRAY
Run Code Online (Sandbox Code Playgroud)

取消引用不同类型

  • 标量参考: $$scalar_ref

  • 数组引用: @$array_ref

  • 哈希参考: %$hash_ref

@_

@_包含参数的别名.修改@_原始值的修改结果.务必尽快制作参数副本并处理这些副本; 您可以安全地修改,而不会更改原始值.

参数和返回值

在Perl中,所有函数调用参数都被展平(折叠)到一个列表(因此失去了它们的身份),返回值也是如此.从功能的角度来看,它是唯一能够接受和一个值的列表,只能返回单个值的列表.

示例场景:

函数的标量参数:

sub foo {
    my $arg = shift;
}
Run Code Online (Sandbox Code Playgroud)

这同样适用于所有参考文献; 引用是标量值.

函数的数组参数:

sub foo {
    my @args = @_;
}

foo( 'bar', 'baz' );
foo( ( 'bar', 'baz' ), ( 'qux', 'spam' ) );  # In no way can the foo subroutine identify that the original arguments were two lists (same with arrays).
Run Code Online (Sandbox Code Playgroud)

函数的哈希参数:

sub foo {
    my %arg = @_;
}

foo( 'bar' => 'baz' );
foo( ( 'bar' => 'baz' ), ( 'qux' => 'spam' ) );  # In no way can the foo subroutine identify that the original arguments were two hashes.
Run Code Online (Sandbox Code Playgroud)

当涉及多个列表(数组)或散列时,始终传递引用.这样,您可以单独识别列表.

$_并且@_不同的

引用您的代码(使用$_不正确):

my($self,$args) = $_ # scalar (this wont work here $args will by undef
Run Code Online (Sandbox Code Playgroud)

$_被称为默认变量,用于许多没有声明显式变量的情况.@_另一方面,它仅用于保存函数内的数组参数(别名).要引用每个元素,我们可以使用:$_[0],$_[1]等等.

参考

  • 您可以从中了解有关Perl中预定义变量的更多信息perldoc perlvar.我总是perldoc -v '$special_variable'从我的终端使用.如果您使用Windows,则单引号必须替换为双引号:perldoc -v "$special_variable".

  • Perl子程序: perldoc perlsub

  • Perl引用和嵌套数据结构: perldoc perlref