in reply to performance - loops, sub refs, symbol tables, and if/elses
In terms of performance, and maintenance, with a variable set of $type's mapping to a variable set of subroutines that need to be invoked, I would combine and enhance two of your approaches to be:
my %dispatch_table = ( 'apple' => [\&a, ...], 'orange' => [\&o, ...], ... ); while (defined($object = read one)) { $_->($object) for @{$dispatch_table{$type}}; }
This approach avoids using a large amount of memory up front to read all objects in to memory, and dispatches using a hash lookup, which will ensure that dispatching is far more linear in terms of number of possible $type values, than would be possible with a string of if, elsif, elsif, ... would be.
Another small trick that I employ above, is to use the suffix modifier form of the for() loop. Although in theory, for(){statement;} should be as fast as statement for();, it is not. The second does not use a block, and avoids some of the block overhead by using simpler op codes underneath.
|
|---|