http://www.perlmonks.org?node_id=1056219


in reply to handling tasks and customizing flow of tasks using perl

Instead of XML, you can introduce simple text format:
task=B pretask=makeAServerdown posttask=checkBServer
Then you can parse it and call pretask/posttask as perl method:
use strict; use warnings; use Data::Dumper; sub makeAServerdown { print "AHA:", @_, "\n"; } my $line = 'task=B pretask=makeAServerdown posttask=checkBServer'; my $h = { map { my ($k,$v) = split /=/; $k=>$v } split (' ', $line) }; print Dumper $h; { no strict 'refs'; $h->{pretask}->($h->{task}); } __END__ $VAR1 = { 'pretask' => 'makeAServerdown', 'task' => 'B', 'posttask' => 'checkBServer' }; AHA:B

Replies are listed 'Best First'.
Re^2: handling tasks and customizing flow of tasks using perl
by asham (Novice) on Sep 29, 2013 at 14:48 UTC
    Hi vsespb, Thanks a lot for sharing your suggestions. I am not sure, if I understood the way you are integrating tasks and customizations but it seems following:
    1) $h (hash) so used in your file(should be standard file?). 2) the module should be then included in task side code where the $h o +f this module will be read.
    Advantage seems to be that xml parser overhead is not required. but then again xml/text have their own pros and cons.
    Some concerns: 1) are you suggesting reading this : "task=B pretask=makeAServerdown p +osttask=checkBServer" from some text file. or hardcoding in code? I a +m assuming text file for now. 2) The customizations suggested by you will work if only known action +is being called. But my use case is that user can customize with anyt +hing they like i.e. they can maybe add any code block they want etc. example of code block: (simple example here) for (int i=0;i<10;i++) { print('just a block for fun: ' . $i . "\n"); }
    Kindly look into this and please correct me as required.
      1) are you suggesting reading this : "task=B pretask=makeAServerdown posttask=checkBServer" from some text file.
      yes
      2) The customizations suggested by you will work if only known action is being called.
      yes

      If you need arbitrary code execution, write an API for programmers.

      I don't see any reason mixing API for programmers and for normal users, nor I don't see use of XML/other formats in API for programmers.

      Or, parhaps, I misunderstand the OP.
        Hi vsespb, thanks for the clarification.

        in my xml, my idea was that normal users will just change the flow if required. expert users will have additional feature to add pretasks/post tasks.

        Idea is to identify a solution which supports plug and play/customization and at the same time provides flexibility to the flow. perl/xml solution is just an idea, we can ignore it if we have better solutions available.

        Btw, updated question with a real life example.