Is it bad practice to share class methods across two packages?

I have one class (ClassOne) that has some code I would like to reuse in another class (ClassTwo). The code does not need an object of ClassOne to work (it just needs some of the data that is an object of class ClassThree, which has an associated list (container) class (ClassThreeList) that holds a list of ClassThree objects). To make the example a little less abstract, ClassThree is a class that manages loading objects of its class from a SQL database.

I really only need one piece of code from ClassOne in ClassTwo, so I was thinking of just "bottling it up" in a class method in ClassOne. Then in package ClassTwo;, use ClassOne; and call that new shared method from ClassTwo as ClassOne->shared_method($data).

I realize that this introduces some coupling between ClassOne and ClassTwo, but are there any obvious reasons not to do something like this? The shared code is code that should be shared so that when it is changed for ClassOne, it also changes for ClassTwo. I.e. ClassOne and ClassTwo would use identical versions of that code, probably forever.
The example below should show the intent more clearly
package ClassOne; sub new { my ($class, %args) = @_; #... my $data = $class->load_a_bunch_of_data($arg{one}); return bless $data, $class; } use ClassThreeList; sub load_a_bunch_of_data { my ($class, $arg1) = @_; # load a bunch of data that only ClassOne cares about # ... # load a list of data that both ClassOne # and ClassTwo care about. my $item_list = ClassThreeList->load_where(objectlistid => $arg1); foreach my $item (@$item_list) { ### BEGIN CODE THAT I WANT TO SHARE ### $item->load_attr1_list; $item->load_attr2_list; $item->calculate_some_attribute. $item->etc; ### END CODE THAT I WANT TO SHARE ### } } # ClassTwo does not exist yet.
The proposed change would be to write ClassTwo and modify ClassOne:
package ClassOne; sub new { my ($class, %args) = @_; #... my $data = $class->load_a_bunch_of_data($arg{one}); return $data, $class; } use ClassThreeList; sub load_a_bunch_of_data { my ($class, $arg1) = @_; # load a bunch of data that only ClassOne cares about # ... # load a list of data that both ClassOne # and ClassTwo care about. my $item_list = ClassThreeList->load_where(objectlistid => $arg1); foreach my $item (@$item_list) { $class->shared_class_method_one( $item ); } } sub shared_class_method_one { my ($class, $item) = @_; $item->load_attr1_list; $item->load_attr2_list; $item->calculate_some_attribute. $item->etc; } package ClassTwo; use ClassOne; sub new { #... } sub new_method_that_needed_shared_code { my ($class, $arg1) = @_; my $item = ClassThree->load(objectid => $arg1); ClassOne->shared_class_method_one($item); ####### LINE IN QUESTION # +####### }

In reply to OOP - Sharing class methods by mp

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.