简体   繁体   中英

Passing data into perl script from command line

I have a perl script the creates a report based on an xml definition. Currently these definitions all exist as .xml files.

So I have the script run-report.pl , which can take a path to a definition file and create the report.

Now I want to create run-reports-from-db.pl , which will generate the report definition based on same database entries. I don't want to create temp files to pass to run-report.pl, I would just like to pass in the definition somehow.

So instead of saying:

run-report.pl -def=./path/to/def.xml

I want to be able to say:

run-report.pl --stream

And have the report definition available in <STDIN>

I am sure there is pretty trivial way to do this???

If I understand your question correctly, all you need is one | (pipe).

./generate-xml-from-db.pl | ./run-report.pl --stream

Anything the first process in the pipeline prints to stdout will appear in the second process's stdin .

As long as you read from STDIN, you have it available. Notice what happens with you take the code below name it something like echo.pl run it at the command line and paste reams of text.

#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;

while ( <> ) { 
    say;
}

<> is the Perl shorthand for "read from STDIN".

As long as the method you're using to launch the process has a way to get a hold of the standard input and outputs, you can just write it to that handle. You have to use the ways that are available to you. In Java, for example, you'd have to get the input stream of the process, in a batch command you have to pipe it. At a GUI terminal you can cut and paste.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM