views:

50

answers:

1

I'm toying with the idea of modifying my home-made test suite to supply it anonymous subroutines as command-line arguments.

The reason why I want to do this is to enhance the script's flexibility. It doesn't seem worth it to hard-code a subroutine that I am only going to use once or twice to assimilate the data in a particular manner.

At the same time I realize that supplying anonymous subs is not a good idea due to a host of issues ranging from syntax-checking to ensuring proper scoping.

The question has two parts:

  • Is this idea sensible?
  • What alternatives can I capitalize on to achieve my stated objective?

The test suite currently runs off the command line (fed in via Getopt::Long).

$ ./testSuite.pl --option params --someFlag --anotherFlag --moreOptions params

Each test generates directories and files, which I want to analyze in one form or another:

  • diff fileA in all directories at the end of the tests
  • Tabulate the data in all fileB's and print to screen
  • Collate an obscure value in fileX of a subset of directories which match required criteria.

The idea of a dispatch table or pre-defined subs appeals if I'm certain of the functionality I need ahead of time. However, this isn't the case here.

A: 

I talk about this quite a bit in Mastering Perl. I'm a big fan of anonymous subs, sometimes past where they make sense. :) What's the goal here? Do you want to give users the flexibility to supply processing or validation routines that they define without your help?

You don't really want to specify the subroutine definition on the command line, do you? Rather, you want to specify the name of the sub to use or how to pull it out of a dispatch table I imagine. Is what I've done in Data::Constraint something like you imagine? Although you probably don't need the module itself, the design is to turn configuration into chains of subroutines.

If you want to let people specify arbitrary callbacks that aren't already coded, would it work for you to let them specify a module to load to supply the sub? They code the tiny module, you load it, and you have access to their subroutine for whatever you want to do. That way they don't type out Perl code on the command line.

brian d foy
@brian : See my updates in the question.
Zaid
In which part of that do you think you need an anonymous sub?
brian d foy
The post-test analysis
Zaid
I think there's a lot that you're leaving out. I don't know what your task is and what sort of setup you have, so "post-test analysis" has no meaning for me. Show a concrete example in your question.
brian d foy