internet_pixie has asked for the wisdom of the Perl Monks concerning the following question:

Appologies for the pun, but as it happens that is my question.

I am writing a Perl application whose default output is XML. What I would like is to provide an extensible feature that allows the user to process this XML.

The use of shell's | is not applicable as multiple XML outputs are possible in a single run (for each command arg there is a XML output).

My initial thought was to pass the path to a Perl script as an arg to my Perl script and then do() the users Perl script. However that doesn't work. I want the code to be executed, not evaluated.

I've thought about using exec(). But I'm not sure that passing a multiline string (that could be 1000's of lines long) is a Good Thing (TM). Nor am I aware of a way of passing a Perl object via the shell.

So I throw this open to you wise brethren. How do I get my Perl script to execute a user provided code given via a command line argument?

Cheers

Internet Pixie

Replies are listed 'Best First'.
Re: should I do() this?
by Joost (Canon) on Aug 19, 2005 at 10:37 UTC
    One possibility is to call an external program for each XML stream, like this simplified example:

    my $remote_program = '/usr/bin/cat'; # set this from the config or pro +gram options for (@output) { # for each xml stream open REMOTE,"|$remote_program" or die "Can't pipe to $remote_progr +am: $!"; print REMOTE $_; # pipe stream to $remote_program close REMOTE; # end }
Re: should I do() this?
by saberworks (Curate) on Aug 19, 2005 at 17:55 UTC
    You can use the unix xargs command to execute a command on each outputted xml file (assuming you can find a way to output them properly):
    xargs reads argu- ments from the standard input, delimited by blanks (which can be pro- tected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by arguments read from standard input. Blank lines on the standard input are ignored.