Chinaunix

标题: 学习mojo源码的问题 [打印本页]

作者: qingyun039    时间: 2018-03-17 14:24
标题: 学习mojo源码的问题
请问mojo::collection 中each方法的 $_->$cb($i++) for @$self中$_不是引用为什么可以这么用?

作者: laputa73    时间: 2018-03-19 08:37
$_为何不是引用?
作者: zhlong8    时间: 2018-03-19 11:25
类方法就是 字符串->method,
可以是直接的 Module::A->method
也可以是 ”Module::A"->method
还可以是是 $m = "module::A"; $m->method;
作者: rubyish    时间: 2018-03-20 08:18
This is Sub Ref As Method

  1. $method->( $head, @tail );
复制代码


EQ

  1. $head->$method(@tail);
复制代码

  1. $_->$cb($i++);
  2. $cb->( $_, $i++ );
复制代码

leisi UFCS (Uniform Function Call Syntax)
https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax

du:
perldoc perlobj

作者: zhlong8    时间: 2018-03-20 11:36
头一次见,还真有这么用的。这两种我都不知道

Subroutine References as Methods
You can also use a subroutine reference as a method:
  my $sub = sub {
      my $self = shift;

      $self->save();
  };

  $file->$sub();
This is exactly equivalent to writing $sub->($file) . You may see this idiom in the wild combined with a call to can :
  if ( my $meth = $object->can('foo') ) {
      $object->$meth();
  }
Dereferencing Method Call
Perl also lets you use a dereferenced scalar reference in a method call. That's a mouthful, so let's look at some code:
  $file->${ \'save' };
  $file->${ returns_scalar_ref() };
  $file->${ \( returns_scalar() ) };
  $file->${ returns_ref_to_sub_ref() };
This works if the dereference produces a string or a subroutine reference.

作者: qingyun039    时间: 2018-03-20 17:27
回复 4# rubyish

十分感谢!!!

作者: rubyish    时间: 2018-04-04 19:35
biru:

  1. push( array, elem )
  2. array.push(elem)
复制代码

  1. $push->( $array, @elem );
  2. $array->$push(@elem);
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2