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

In reply to Re^4: Passing a structure from C to Perl by bliako
in thread Passing a structure from C to Perl by drpraveena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.