in reply to Process order

How's this?
#!/usr/bin/perl use strict; use warnings; my $input = 'a'; my %po = ( PR1 => 2, PR2 => 4, PR3 => 1, PR4 => 3, ); foreach my $key (sort { $po{$a} <=> $po{$b} } keys %po) { $key =~ s/^PR/process/; $input = eval "$key(\$input)" or die $@; print $input . $/; # debugging output } sub process1 { lc(shift) . 1 } sub process2 { lc(shift) . 2 } sub process3 { uc(shift) . 3 } sub process4 { uc(shift) . 4 }


Output:
A3 a31 A314 a3142


Update: eval can be dangerous! Use this solution only if you are sure that the value of $input is safe! Taint mode highly recommended.

---
It's all fine and dandy until someone has to look at the code.

Replies are listed 'Best First'.
Re^2: Process order
by ikegami (Patriarch) on Jun 09, 2006 at 16:15 UTC

    $input = eval "$key(\$input)" or die $@;
    can be replaced with the safer
    $input = do { no strict 'refs'; &{$key}($input) };
    A dispatch table would be even safer.

Re^2: Process order
by anniyan (Monk) on Jun 09, 2006 at 15:19 UTC

    Zaxo, davorg and kwaping thanks for your wonderful replies and i sorted out my problem by the ideas given by you monks.

    ikegami, Zaxo added the coding part after i posted the reply. :-)

    Regards,
    Anniyan
    (CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)