in reply to performance - loops, sub refs, symbol tables, and if/elses

Form a maintainable view, I would suggest you, to put things in OO style:
workflow.pm: package workflow; use strict; sub new { my $self = {}; $self->{TASKS} = []; bless $self; return $self; } sub add_task { my $self = shift; push @{$self->{TASKS}}, shift; } sub do_it { my $self = shift; my $item = shift; foreach my $task (@{$self->{TASKS}}) { &{$task}($item); } } 1; workflow_table.pm: package workflow_table; use workflow; use strict; sub new { my $self = {}; $self->{WORKFLOWS} = {}; bless $self; return $self; } sub add_task { my $self = shift; my $type = shift; my $task = shift; if (!defined($self->{WORKFLOWS}->{$type})) { $self->{WORKFLOWS}->{$type} = new workflow; } $self->{WORKFLOWS}->{$type}->add_task($task); } sub do_it { my $self = shift; my $type = shift; my $item = shift; $self->{WORKFLOWS}->{$type}->do_it($item); } 1; test.pl use workflow_table; use Data::Dumper; use strict; sub apple_wash {print "wash apple ".shift()."\n";} sub apple_core {print "core apple ".shift()."\n";} sub apple_pulp {print "pulp apple ".shift()."\n";} sub banana_bend {print "bend banana ".shift()."\n";} sub banana_hang {print "hang banana ".shift()."\n";} my $t = new workflow_table; $t->add_task("apple", \&apple_wash); $t->add_task("apple", \&apple_core); $t->add_task("apple", \&apple_pulp); $t->add_task("banana", \&banana_bend); $t->add_task("banana", \&banana_hang); $t->do_it("banana", "banana1"); $t->do_it("banana", "banana2"); $t->do_it("apple", "orange?");

Replies are listed 'Best First'.
Re: Re: performance - loops, sub refs, symbol tables, and if/elses
by Elian (Parson) on Dec 17, 2002 at 17:08 UTC
    Except that the OO style has a significant performance penalty relative to the sub style, and the idea here is for performance....
        /msg jdporter That's because they don't trust anyone who's not drunk the OO Kool-aid...

        :-)