What should I use: qx or system?

A

Andreas Kienle

Hi,

I have the following problem: I want to create a perl script which
will be used by several other scripts. The perl script should return a
string and print something else to STDOUT.
For example it should read a line from a file and return this to the
calling script but printing "Read from file 'bla'" to STDOUT
When I use system() to call the script then the printing to the screen
works as usual but the main program cannot access the retrieved
string. On the other hand when I use `` I can save the result in a
variable but cannot print to the screen from the "subscript".

I hope you understand what I mean


Thanks for any help!
- Andreas
 
A

Anno Siegel

Andreas Kienle said:
Hi,

I have the following problem: I want to create a perl script which
will be used by several other scripts. The perl script should return a
string and print something else to STDOUT.

That's impossible. The string an external command returns via qx *is*
what it prints to STDOUT. A program can't return one thing and print
another.
For example it should read a line from a file and return this to the
calling script but printing "Read from file 'bla'" to STDOUT
When I use system() to call the script then the printing to the screen
works as usual but the main program cannot access the retrieved
string. On the other hand when I use `` I can save the result in a
variable but cannot print to the screen from the "subscript".

You could print whatever you only want to see on the screen to STDERR,
while printing the return value to STDOUT. qx and friends only catch
STDOUT. But that is only portable to environments that *have* STDERR.

Anno
 
B

Ben Morrow

Quoth (e-mail address removed) (Andreas Kienle):
I have the following problem: I want to create a perl script which
will be used by several other scripts. The perl script should return a
string and print something else to STDOUT.
For example it should read a line from a file and return this to the
calling script but printing "Read from file 'bla'" to STDOUT
When I use system() to call the script then the printing to the screen
works as usual but the main program cannot access the retrieved
string. On the other hand when I use `` I can save the result in a
variable but cannot print to the screen from the "subscript".

I hope you understand what I mean

No. I don't think you do, either.

A (C, under Unixish and related OSen) program produces two forms of
output: data written to filehandles and its exit code. The exit code is
a number from 0 to 32767-ish. STDOUT is a filehandle: you can write data
to it just like any other. If you run a program with qx//, perl will
catch all the data that program writes to STDOUT and return it to you:
there is no way for the program to 'return' a separate stream of data.

I think what you want to do is print your 'Read from file' message to
STDERR, not STDOUT. Then print your output string to STDOUT and run the
program with qx//. This is why there is a separate standard error
stream: so that a program can pass separate messages to a 'calling'
program and the user.

Ben
 
J

Joe Smith

Andreas said:
I have the following problem: I want to create a perl script which
will be used by several other scripts. The perl script should return a
string and print something else to STDOUT.

In that case, you chould have the calling program open a file descriptor
such as descriptor number 4 (anything but 0, 1, 2) and have the called
program write its result to that file descriptor. You could use a pipe
for this purpose.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top