I would like to know if it is possible to call a method from an object without passing the self argument.
As an example, I have a package:
package MyPackage;
sub new {
my $class = shift;
return bless {}, $class;
}
sub test {
print("called(" . join(', ', @_) . ")\n");
}
From a script, I call the constructor and then the test
method:
my $obj = MyPackage->new();
$obj->test("str");
giving me the following output:
called(MyPackage=HASH(0x55b05d481f48), str)
Is there any way (even if it's not a best practice or use some "arcane" features of the language) to call the test
method using only the reference $obj
without having the "self"-parameter passed implicitly.
In a word, is it possible to do something like this:
$objXXXXtest("str");
with XXXX
the hypothetical construct and get called(str)
as output?
它有点非正统,但UNIVERSAL::can
返回一个代码引用,你可以在没有引用的情况下调用它。
$obj->can("test")->("str");
您只需直接调用该函数,而不是作为对象的方法:
myPackage::test("str");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.