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

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -