What syntax is used for sub foo: method {shift-> bar (@_)}?

sub foo : method { shift->bar(@_) } 

What does : method mean?

I never used it that way ...

+7
source share
1 answer

: method attribute description . A routine that is marked this way will not trigger the "Ambiguous call resolved as CORE ::% s" warning.

From ysth comment :

A warning occurs when sub has the same name as the built-in, and it is called without and not as a method call; perl uses the built-in instead, but gives a warning. Method: quiets warns a warning because it clearly indicates that sub was never intended to be called as a non-method in any case.

Update

This code simply calls the bar method when foo is called:

 sub foo : method { ## Mark function as method shift->bar(@_) ## Pass all parameters to bar method of same object } 

More details:

  • : method - indicates that the routine referenced is a method. A routine that is marked this way will not trigger the "Ambiguous call resolved as CORE ::% s" warning.
  • shift - gets the first parameter from @_ , which will be $self
  • ->bar(@_) - call the same bar class method with all other parameters

You can read this as:

 sub foo : method { my ($self) = shift @_; return $self->bar(@_); } 
+11
source

All Articles