Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

call python from perl cause error

by anaconda_wly (Scribe)
on Aug 09, 2013 at 06:33 UTC ( #1048694=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

My Perl script for this test is very simple, just for test:

my $mycmd = "test.py";#test.pl my @res = qx/$mycmd/;

My test.py script is very simple too:

#!/usr/bin/env python import sys import os os.system("ping 127.0.0.1 > NUL")

execute perl test.pl produce error on Win2003: The handle could not be opened during redirection of handle 1.

No error message on win2008.

From some search on net I guess it's may because when calling the python, the standard output is not created. Any way to modify the Perl script to eliminate the error? Assuming the python file can not be modified.

Comment on call python from perl cause error
Select or Download Code
Re: call python from perl cause error
by Anonymous Monk on Aug 09, 2013 at 08:19 UTC

    produce error

    What means produce?

    Any way to modify the Perl script to eliminate the error?

    No. Consider

    $ perl -e " $foo = qx{perl -e \x22system q/ping 127.6.6.6 >NUL/\x22} " $ $ perl -e " $foo = qx{perl -e \x22system q/ping 127.6.6.6 >&NUL/\x22} +" >& was unexpected at this time. $ perl -e " $foo = qx{perl -e \x22system q/ping 127.6.6.6 &>NUL/\x22} +" The syntax of the command is incorrect.

    The error messages produced are what cmd.exe spits out on STDERR when invoked via system

    Now you might be able to consume STDERR with Try::Tiny, or simply redirect it with  2>NUL ...

    Does that work for you?

    Treasure trove of shell links

      Not just to eliminate the error message, I'd like not to produce the error.

      On win2k3, when from the Perl process starting another Python process, and in Python execute redirection commands, this error happens.

      When I type "test.py" in cmd promp and execute, will execute well without error; When I execute "perl test.pl" to call it, the error happens......

        Not just to eliminate the error message, I'd like not to produce the error.

        First you have to figure out which program/process produces the error

Re: call python from perl cause error
by 2teez (Priest) on Aug 09, 2013 at 08:56 UTC

    Hi anaconda_wly,
    Assuming the python file can not be modified
    Then:

    1. You might have to do ... qx/python $mycmd/; in your perl script for it to work.
    2. You will want to use system, instead of backticks and "qx//". Since your python script is redirecting the output of the program into a file named 'NUL'.
      But if what you want is output in your array "@res", then you might have to modify your python script not to 'redirect' using backtick or 'qx//' like you did.
    3. Using your python script on linux OS, since I don't have a Win OS to try it on. Will make the script run almost forever unless the "ping" command in the python script is given a "count" like so: .. "ping -c 10 127.0.0.1 > NUL".

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: call python from perl cause error
by syphilis (Canon) on Aug 09, 2013 at 11:26 UTC
    On Windows 7 I can't reproduce the exact problem, but odd things seem to happen with the output when perl calls python.
    For example, if, in the python script, I *don't* redirect to NUL then I expect to see the output of the ping command in the console when I execute test.py ... and that happens.
    I also expect to be able see the output of the ping command in the console when I run test.pl ... but it doesn't appear. If I change test.pl so that it uses backticks to execute test.py, then I *still* don't see the output of the ping command. But if I change test.pl so that it uses system() to execute test.py, then I *do* see the output of the ping command displayed in the console.

    (On the basis if this, I'm wondering whether using system() might improve things for you.)

    If I want to use either backticks or qx// to execute test.py, then in order to be able to view the output of the ping command, I have to change it to os.system("ping 127.0.0.1 1>&2")
    So, STDOUT is definitely being messed with when perl executes python ... but I don't know the details ... and couldn't find anything readily digestible on the subject on google.

    2teez ... if you're wondering ... on Windows the ping command sends just 4 pings and exits, by default ;-)

    Cheers,
    Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1048694]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-12-20 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls