I've read off and on that when you make an object oriented package, that you should not force poop on people. That it should be an opton, not a default.

So, my first thoughts were ... I guess if my package has method hello(), then there should be a _hello() which is the functional oriented equivalent. Seems silly.. And I have a gut feeling there's various tricks to have a sub act as both a functional oriented, and object oriented subroutine.

I patched this together and it seems to work well. Here's my example:

use Carp; use constant haveyaml => ( eval 'require YAML;' ? 1 : 0 ); my $META_HIDDEN=1; sub META_HIDDEN : lvalue { $META_HIDDEN } my $META_EXT = 'meta'; sub META_EXT : lvalue { $META_EXT } sub set_meta { my $abs_path = shift; if (ref $abs_path eq __PACKAGE__){ $abs_path = $abs_path->abs_path; } my $meta = shift; ref $meta eq 'HASH' or croak('second argument to set_meta() must be a hash ref'); haveyaml or carp 'set_meta() cant be used, ' .'YAML is not installed.' and return; $abs_path=~s/^(.+\/)([^\/]+$)/$1.$2/ if META_HIDDEN; unless( keys %$meta){ unlink $abs_path .'.'.META_EXT; return 1; } YAML::DumpFile($abs_path .'.'.META_EXT,$meta); return 1; }

So this sub can be used as $object->set_meta($hashref) or set_meta($path,$hashref). The way to detect here is to see if the first argument is a ref to the package, or not.

I've looked for examples of having subroutines act as both object oriented and functional oriented code, and I came up kind of empty, maybe I'm not looking right.

Any suggestions how else to do this? How to have a subroutine detect if it's being used as a method or a normal subroutine?

And uhm.. If I can also ask.. Is setting that constant to check for YAML really bad? Or too taxing on the sytem?

update
Using a subroutine as both procedural function and oo method can create problems with inheritance (and complexity). Thanks to the discussion, I can with confidence put this aside and move on. Verdict is nonono. I'll use different subs, some for oo some for procedural.


In reply to 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.