How can I write a Perl subroutine that works both as a normal subroutine or a class method? -
I am writing a function that is more stable in nature. I want to plug it into the Template Toolkit, which passes through the class name.
But I want to do something about it
classname-> function ($ args ..)
className :: function ($ args ..)
inside
sub-function {}
< / Pre>What is the correct way to handle both cases?
Normally, it is not. Sub
is either called as a method or it is not.
See how this situation is prepared before the package name in the logic list.
Now, In a very specific, limited case , you can:
shift $ $ [0] eq __PACKAGE__;
When sub
as the first line in your code is called as a class method, leave the argument first.
Comments
Post a Comment