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

Hi,

I am trying to read a directory containing XML and convert it to PDF using XSLFOP

I have managed to collect a list of XML files that meet a certain criteria and generate a list of corresponding output files.

I am trying to use FOP calling it thru eval but I get errrors generated by FOP.

When I substitute my variable with hard values the eval statement executes without error and an pdf is generated. I suspect my eval is sending the wrong parameters to the command line.

Can anybody tell me in a flash what is wrong with this snippet of code?????????

eval{ ` /usr/local/src/fop-0.20.4/fop.sh -xsl xslfo.xsl -xml ` . $ +inputfile . ` -pdf ` . $outputfile; }

Replies are listed 'Best First'.
Re: Problems with eval
by Aristotle (Chancellor) on Sep 06, 2002 at 15:23 UTC
    It captures the output from /usr/local/src/fop-0.20.4/fop.sh -xsl xslfo.xsl -xml , appends the value of $inputfile to the result, tries to append the output from a nonexistant program called -pdf to that, and finally appends the contents of $outputfile to that. What you want is
    eval { `/usr/local/src/fop-0.20.4/fop.sh -xsl xslfo.xsl -xml $inputf +ile -pdf $outputfile`; }
    Perl will interpolate its own variables into that string before calling the shell. Actually, a much better way here is system qw(/usr/local/src/fop-0.20.4/fop.sh -xsl xslfo.xsl), -xml => $inputfile, -pdf => $outputfile;

    Makeshifts last the longest.

Re: Problems with eval
by BrowserUk (Patriarch) on Sep 06, 2002 at 16:11 UTC

    It's also worth pointing out that

    • you don't need to use curly braces to bracket an arguement to eval if that arguement is a simple string.
    • and you don't need to eval a back-ticked string.

      When perl encounters a back-ticked string, it passes the contents of the string (after interpolating any embedded vars) to a system shell for processing, captures any output from the command, and returns that to the calling program once the command completes. eg.

      print "`set tmp` returned:", `set tmp`, $/;

      results in

      `set tmp` returned:TMP=D:\TEMP

      on my system.

      If you need to know the exit status of the command being issued, then you need to look at $? see perlman:perlvar. eg.

      print "`set tmp` returned:", `set tmp`, " and had an exit code of:$?", +$/; print "`set non-existant-env` returned:", `set non-existant-env`, " an +d had an exit code of:$?",$/;

      gives

      `set tmp` returned:TMP=D:\TEMP and had an exit code of:0 Environment variable non-existant-env not defined `set non-existant-env` returned: and had an exit code of:256

    Finally, you're almost certainly better of using the system function (see perlamn:perlfunc), as Aristotle suggest's, but don't be confused by the => ! This is colloqually known as 'the fat comma', and when used as Aristotle has, it is simply equivalent to a comma, and is used here rather than a comma simply for emphasis. I point this out as I spent several hours searching the docs trying to find out what it meant when I first encountered it in a reply to one of my questions.


    Well It's better than the Abottoire, but Yorkshire!
      Actually I despise using it purely for emphasis outside hash assignments when the left side is not an autoquoted bareword. :-) In this case I could have written '-xml', $inputfile, '-pdf', $outputfile but that's a lot more line noise IMHO. I hate seeing it used like push @array => $scalar; though which I've occasionally seen here.

      Makeshifts last the longest.

        Nods! I think it was a use close to that had me running in mental circles for a couple for hours when I first started.


        Well It's better than the Abottoire, but Yorkshire!