What should I use: qx or system?

Discussion in 'Perl Misc' started by Andreas Kienle, Jul 22, 2004.

  1. 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
    Andreas Kienle, Jul 22, 2004
    #1
    1. Advertising

  2. Andreas Kienle

    Anno Siegel Guest

    Andreas Kienle <> 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
    Anno Siegel, Jul 22, 2004
    #2
    1. Advertising

  3. Andreas Kienle

    Ben Morrow Guest

    Quoth (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.
    Frank Herbert, 'Dune'
    Ben Morrow, Jul 23, 2004
    #3
  4. Andreas Kienle

    Joe Smith Guest

    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
    Joe Smith, Jul 25, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GASOLINE
    Replies:
    0
    Views:
    307
    GASOLINE
    May 8, 2004
  2. Lacrima
    Replies:
    43
    Views:
    785
    Mark Lawrence
    Aug 2, 2010
  3. ~~~ .NET Ed ~~~

    How should control images should be handled?

    ~~~ .NET Ed ~~~, Oct 31, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    218
    John Saunders
    Nov 3, 2004
  4. Josef 'Jupp' SCHUGT

    What the FAQs should and should not contain

    Josef 'Jupp' SCHUGT, Aug 19, 2005, in forum: Ruby
    Replies:
    0
    Views:
    177
    Josef 'Jupp' SCHUGT
    Aug 19, 2005
  5. botp
    Replies:
    6
    Views:
    192
    Joel VanderWerf
    Oct 5, 2010
Loading...

Share This Page