Home > Archive > PERL Miscellaneous > July 2004 > What should I use: qx or system?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
What should I use: qx or system?
|
|
| Andreas Kienle 2004-07-22, 8:56 am |
| 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
| |
| Anno Siegel 2004-07-22, 3:57 pm |
| Andreas Kienle <andreaskienle@gmx.de> wrote in comp.lang.perl.misc:
> 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
| |
| Jim Gibson 2004-07-23, 8:56 pm |
| In article <60233f21.0407220329.19150b11@posting.google.com>, Andreas
Kienle <andreaskienle@gmx.de> wrote:
> 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".
From what you describe, there is no need to run your common script in a
separate process. Include it as a subroutine in any program that wants
to use it. Look up Perl functions 'do', 'require', and 'use' to see how
to do that in several flavors of modularity. Put your common routine in
its own file and use one of the functions mentioned to include it in
your programs.
| |
| Joe Smith 2004-07-28, 9:00 pm |
| Andreas Kienle wrote:
> 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
| |
| Ben Morrow 2004-07-28, 9:00 pm |
|
Quoth andreaskienle@gmx.de (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
--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
ben@morrow.me.uk Frank Herbert, 'Dune'
|
|
|
|
|