in reply to Re^2: multiple inheritance alternative(s)?
in thread multiple inheritance alternative(s)?

To add an example, I have a toy project Filesys::DB, and there are various maintenance operations for the database:

  1. Scan new entries
  2. Scan existing entries for changes
  3. Maintenance of cache tables
  4. ...

First I also had these within the scripts implementing these operations. Then I moved them into Filesys/DB.pm, and now I have Filesys/DB/Operation.pm, in which these operations live. In the longer run, the operations will likely move into their own sub-files.

As an example, moving a method from the main class into another module/class can be done like this with Moo:

# Old version package My::Example; use 5.020; use Moo 2; use experimental 'signatures'; sub do_foo( $self, %options ) { ... }
# New version version package My::Example; use 5.020; use Moo 2; use experimental 'signatures'; has 'operation_foo' => ( is => 'lazy', default => sub { My::Example::Operation::Foo->new(), }, handles => ['do_foo'], ); package My::Example::Operation::Foo; use 5.020; use Moo 2; use experimental 'signatures'; sub do_foo( $self, %options ) { .... }

Using objects for holding code mostly makes sense if the operations don't really need information that lives in the "parent" class.

Update: choroba spotted a misspelled does where it should have been handles.

Replies are listed 'Best First'.
Re^4: multiple inheritance alternative(s)?
by Danny (Chaplain) on Oct 10, 2024 at 23:39 UTC
    I'll have to look more into the Moose stuff, but my first impression is "yuck".

      ... which is why I prefer Moo, which is a lot lighter.