With a single-parameter JSON string you can pass a very complex parameters structure which can be parsed programmatically at the perl side (of course you may need to intervene to extend your perl-side parser if you change structure of params string and for params checking).
If you use JSON you can pass very complex data structures including arrays, arrays of arrays, hashes, arrays of hashes, etc. the sky is the limit.
You asked:
"Instead of passing individual parameters to the script, I would like to pass a structure with all these parameters and keep extending the structure as and when I need to pass more parameters."
Which is exactly what I am saying (borrowing Corion's JSON idea). In the simplest scenario, you can pass a JSON array of arbitrary length for your parameters which is equivalent to running your perl script with an arbitrary length of arguments.
But when you want to add more structure to your parameters (e.g. the hashes above) then it will be a bit painful to do that via the script arguments.
And lest not forget about parameter checking at perl side (will be needed if from time to time a human runs the script too). If you want to do that, you have to modify the logic each time your parameters spec changes (well not necessarily but usually). Now with JSON (string or file) I can see a way where you also pass the logic for checking your parameters, as a perl sub for example. Eval is very dangerous (and in many security scenarios forbidden - e.g. if that is part of a web/CGI then forget it) but I am enumerating some possibilities without knowing much about your program's environment ...
Jockingly: I can see someone creating the whole perl script from within C, from scratch or from a template, where the logic of parameter checking (perl side) is programmatically created and inserted from within the C program into the perl program, every time you change params specs in the C program.
Suddenly all the logic moves to the C program.
Or, from within C, insert(=hardcode) all the parameter values where they are supposed to go in the perl script on the fly, from a template. In this way a perl script for one-time usage is created with all initial state (params) hardcoded in it. And all params-checking is done exclusively in C. Very clean indeed. Good for running as a pipeline in a parallel environment. And you can re-run the program for debugging the pipeline without being concerned about parameter strings or files.
Again, if you give more details I can be more specific.
regards, bliako
|