(I suspect you meant to say "imperative programming" rather than "functional programming", so I'm gonna run with that assumption.) There are plenty of examples of this behavior. (Forgive me if I don't name any.) I don't think it has to end up messy or troublesome at all. Here's a short example of one such setup.

In Foo.pm:

#!/usr/bin/perl use strict; use warnings; package Foo; sub new { my $class = shift; bless { name => 'foo' }, ref($class)||$class; } sub bar { my $ref = ref $_[0]; if ($ref eq __PACKAGE__) { ## OO method call oo_bar (@_); } elsif ($ref eq 'HASH') { ## imperative subroutine call with hashref as first element im_bar (@_); } } sub oo_bar { my $self = shift; print "oo_bar got object named ", $self->{name}, "\n"; } sub im_bar { my $hr = shift; print "im_bar got hashref with name ", $hr->{name}, "\n"; } 1;
In im-oo-test.pl:
#!/usr/bin/perl use strict; use warnings; use Foo; my $hr = { name => 'hashref' }; my $obj = new Foo; ## OO method call $obj->bar; ## imperative call, but since $obj is a blessed object, ## this is equivalent to the OO call Foo::bar($obj); ## imperative call Foo::bar($hr);
There's a zillion ways to proceed from this skeleton. You could use a naming scheme like Module::OO::sub() rather than oo_sub(), you could write one subroutine to handle both types of calls, you can make the ref $_[0] business a little cleaner with use UNIVERSAL 'isa'... you can even ignore whether a hashref is an object, provided it has the right keys and values in it.

I would consider it good programming practice to put maximum flexibility into how your subroutines may be called, as long as this flexibility doesn't introduce too much complexity on the backend. That's your call.


In reply to Re: Is it ok to mix functional and oo programming in one package? by gamache
in thread Is it ok to mix functional and oo programming in one package? 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.