- 论坛徽章:
- 46
|
头一次见,还真有这么用的。这两种我都不知道
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. |
|