Leaving the debate aside for the moment whether this is a good idea in principle, your code as given doesn't support inheritance. When you test to see if you are called as a function or a method, you do
if ( ref $abs_path eq __PACKAGE__ ) {
That won't do. If another class inherits from yours, it will have a different name but its objects have every right to call your method. Your code doesn't let them. The problem is similar to calling bless() with only one argument in new().

Elsewhere in the thread jdporter has mentioned the problem in passing:

I should also say that I don't know how well this will hold up under inheritance. You'd probably have to tweak certain things, i.e. rather than string test against __PACKAGE__, test using isa or can.

Using isa( __PACKAGE__) is a possibility. It will make inheritance work, but in the inheritance case it really does too much. The code has been called because the object "is a" __PACKAGE__ object by inheritance, so why check again?

There is also the possibility that the method was called fully qualified with an object of an arbitrary class, as in $obj->Your::Class::set_meta( ...). In this case, even the isa() test would reject what is a legitimate call. If the programmer thinks the object will support the method, she will probably know what she's doing.

So I suggest simply testing for object-ness

if ( Scalar::Util::blessed( $abs_path) ) {
The discussion also shows how easy it is to break OO capabilites while adapting code so that methods can also be called as ordinary subs. I believe, if you want this capability you'd be better off with a pure OO class, plus an adapter module (possibly AUTOLOADER-driven) that arranges for calls-as-functions.

Anno


In reply to Re: coding a subroutine as both method and regular function by Anno
in thread coding a subroutine as both method and regular function by leocharre

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.