in reply to Newbie OO module-structure question

You didn't provide enough information for a detailed answer. We don't know how the tasks depend on each other, what parts of the code are common to the tasks, etc. But here is a short example (using Moo) that just stores the tasks in the "tasks" attribute and runs them one by one. All the tasks share a role that says they have a name and must be runnable. A possible next step might be the introduction of the Factory pattern to build the tasks...

#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; { package My::Task::Runner; use Moo; has tasks => (is => 'ro'); sub run_tasks { $_->run for @{ shift->tasks }; } } { package My::Task; use Moo::Role; requires 'run'; has name => (is => 'ro', required => 1); } { package My::Task1; use Moo; with 'My::Task'; sub run { say "Running: ", shift->name; } } { package My::Task2; use Moo; with 'My::Task'; has mode => (is => 'ro'); sub run { my ($self) = @_; say "Running: ", $self->name, ' in ', $self->mode, ' mode.'; } } my $runner = 'My::Task::Runner'->new( tasks => [ 'My::Task1'->new(name => 'One'), 'My::Task2'->new(name => 'Two', mode => 'LOUD'), ], ); $runner->run_tasks;
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Newbie OO module-structure question
by LanX (Saint) on Apr 30, 2019 at 00:29 UTC
    Nice!

    ... but can you introspect the tasks with My::Task role, in order to DRY°?

    update

    In other words, is it possible to loop over all classes which have a role instead of hardcoding it at the end?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    °) don't repeat yourself

    update

    fixed auto-correction: s/introspective/introspect/

      I don't see any loop over classes. There's a loop over objects.

      If you don't like the constructors of the particular TaskN objects, that's what I'd solve with a factory.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        > If you don't like the constructors of the particular TaskN

        I didn't say I don't like it, I was asking if it's possible to get a list of all classes having a specific role.

        Probably too far fetched for Moo.

        > that's what I'd solve with a factory.

        Good point!°

        On a side note ...

        Could you please write an OO book for languages with dynamic typing?

        I'm tired of the cognitive dissonance caused by Java concepts. ;D

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        °) the OP is so wishy washy that I was thinking in another direction

      "...in order to DRY...have a role instead of hardcoding...

      Insisting so much on DRY is sometimes remote from everyday life. Because RY is human nature. Why? Remember how often your parents, siblings, aunts, uncles, spouses, colleagues, fellow monks etc. told you to become a better person. In the long run they have been successful. Sometimes WET is better. Or faster - as you like.

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        Is it already time to miss s*nd*l?

        So soon ... ;-P

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice