At one point during my development of Hashids for Perl I thought about something like this, especially for some utility methods that I ported from the original JavaScript source. On one hand, there's a "seductive" thought of making all things OO-ish for a "consistent" interface, but ultimately I found that the utility methods were, after all, utility, and could be plausibly used out of the Hashids object context.

So in the end, I decided to make a Hashids::Util module that implements subs only, not methods; just add a bit of Exporter magic to provide the subs on by-name basis (or use Hashids::Util ':all' if you want them wholesale) and namespace::clean on both the module and requestors so that those subs don't pollute the namespace.

Here's a basic template for such a Util module that you might want to use:

package Util; use strict; use warnings; our ( @EXPORT_OK, %EXPORT_TAGS ); BEGIN { use Exporter 'import'; @EXPORT_OK = qw(foo bar baz quux); %EXPORT_TAGS = ( all => \@EXPORT_OK ); } # Dependencies for this Util module go here... use namespace::clean -except => [qw(import)]; sub foo { my ( $x, $y, @z ) = @_; ...; } sub bar { 42 } sub baz { ... } sub quux { ... } "Derp."; __END__
I suspect that another possible way for you would be to go implement those utility methods in a Role, and compose this Role in to your other classes; that might be a better fit for your situation, as you mention in another reply ("The base ideas was to create a class 'Object' which all classes inherit from.") We could turn the example above into something like this (assuming using Moo for OO, change needed if its something else:)
package Class::Util; use Moo::Role; sub foo { my ( $self, $x, $y, @z ) = @_; ...; } has bar => ( is => 'ro', default => sub { 42 } ); ...; 1;

Then in your classes you just add with 'Class::Util';.

EDIT: I realized you can actually combine the two approaches above rather easily (though at a cost of a bit of conceptual overhead.) Here's one rather naive approach:
package Class::Util; use Moo::Role; use Util (); for my $method (@Util::EXPORT_OK) { no strict 'refs'; *$method = sub { my $self = shift; my $sub = "Util::$method"; $sub->(@_); }; } "Derp.";

Now you can have a regular Util module for standalone subs, and a Class::Util role for objects.


In reply to Re: Perl OOP Function Entrance by zakame
in thread Perl OOP Function Entrance by Mano_Man

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.