FAQ
Ok ..... so I have to start a program which is written in java.

#!/local/bin/perl

my $command = "/aa/bb/c/executable \&";
my $ret = 0;

$ret = `$command`;

exit $ret;

The normal behavior of this executable is to list some information but
it never returns you to a prompt. You have to hit the return key to
get the shell prompt back.

The same thing is happening to this program. Is there a way in perl
to pass a return "\n" on the command to be executed that would return
a prompt to the program so it does not hang? I have tried the
following:

"/aa/bb/c/executable \& \; echo 100";

The shell doesn't like " \; echo 100" at all. Any help would be
greatly appreciated. I have several programs to start ... all are
currently hanging.

Bill

Search Discussions

  • Chas. Owens at Feb 7, 2009 at 12:17 pm

    On Fri, Feb 6, 2009 at 14:31, Ice Man wrote:
    Ok ..... so I have to start a program which is written in java.

    #!/local/bin/perl

    my $command = "/aa/bb/c/executable \&";
    my $ret = 0;

    $ret = `$command`;

    exit $ret;

    The normal behavior of this executable is to list some information but
    it never returns you to a prompt. You have to hit the return key to
    get the shell prompt back.

    The same thing is happening to this program. Is there a way in perl
    to pass a return "\n" on the command to be executed that would return
    a prompt to the program so it does not hang? I have tried the
    following:
    snip

    It sounds like you need IPC::Open2* or IPC::Open3**. They allow you
    to run an external command and control its STDIN, STDOUT, and, in the
    case of IPC::Open3, STDERR. Try something like this:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use IPC::Open2;

    #if this is the child
    my $arg = shift;
    if (defined $arg and $arg eq 'you are the child') {
    print "I am the child\nwaiting for user to hit enter...\n";
    <STDIN>;
    exit;
    }

    #if this is the parent
    print "I am the parent, getting ready to run the child\n";
    #call myself with the argument "you are the child"
    open2 my $child_stdout, my $child_stdin, $0, "you are the child";

    print "child ran, sending return to it now\n";
    #send a return to the child to get it to quit
    print $child_stdin "\n";

    #read what it wrote to the stdout
    print "child said:\n", map { "\t$_" } <$child_stdout>;



    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Chas. Owens at Feb 7, 2009 at 12:19 pm

    On Sat, Feb 7, 2009 at 07:17, Chas. Owens wrote:
    On Fri, Feb 6, 2009 at 14:31, Ice Man wrote:
    Ok ..... so I have to start a program which is written in java.

    #!/local/bin/perl

    my $command = "/aa/bb/c/executable \&";
    my $ret = 0;

    $ret = `$command`;

    exit $ret;

    The normal behavior of this executable is to list some information but
    it never returns you to a prompt. You have to hit the return key to
    get the shell prompt back.

    The same thing is happening to this program. Is there a way in perl
    to pass a return "\n" on the command to be executed that would return
    a prompt to the program so it does not hang? I have tried the
    following:
    snip

    It sounds like you need IPC::Open2* or IPC::Open3**. They allow you
    snip

    Too early in the morning, forgot the footnotes:

    * http://perldoc.perl.org/IPC/Open2.html
    ** http://perldoc.perl.org/IPC/Open3.html

    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Ice Man at Feb 7, 2009 at 2:41 pm

    On Feb 7, 7:17 am, chas.ow...@gmail.com (Chas. Owens) wrote:
    On Fri, Feb 6, 2009 at 14:31, Ice Man wrote:
    Ok ..... so I have to start a program which is written in java.
    #!/local/bin/perl
    my $command = "/aa/bb/c/executable \&";
    my $ret = 0;
    $ret = `$command`;
    exit $ret;
    The normal behavior of this executable is to list some information but
    it never returns you to a prompt. You have to hit the return key to
    get the shell prompt back.
    The same thing is happening to this program. Is there a way in perl
    to pass a return "\n" on the command to be executed that would return
    a prompt to the program so it does not hang? I have tried the
    following:
    snip

    It sounds like you need IPC::Open2* or IPC::Open3**. They allow you
    to run an external command and control its STDIN, STDOUT, and, in the
    case of IPC::Open3, STDERR. Try something like this:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use IPC::Open2;

    #if this is the child
    my $arg = shift;
    if (defined $arg and $arg eq 'you are the child') {
    print "I am the child\nwaiting for user to hit enter...\n";
    <STDIN>;
    exit;

    }

    #if this is the parent
    print "I am the parent, getting ready to run the child\n";
    #call myself with the argument "you are the child"
    open2 my $child_stdout, my $child_stdin, $0, "you are the child";

    print "child ran, sending return to it now\n";
    #send a return to the child to get it to quit
    print $child_stdin "\n";

    #read what it wrote to the stdout
    print "child said:\n", map { "\t$_" } <$child_stdout>;

    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
    Excellent !!! This looks like my answer! Will attempt to implement on
    09-Feb [Monday] and let you know.

    Thanks for your quick reply!
  • Ice Man at Feb 10, 2009 at 10:40 am

    On Feb 7, 9:40 am, wjharris...@optonline.net (Ice Man) wrote:
    On Feb 7, 7:17 am, chas.ow...@gmail.com (Chas. Owens) wrote:


    On Fri, Feb 6, 2009 at 14:31, Ice Man wrote:
    Ok ..... so I have to start a program which is written in java.
    #!/local/bin/perl
    my $command = "/aa/bb/c/executable \&";
    my $ret = 0;
    $ret = `$command`;
    exit $ret;
    The normal behavior of this executable is to list some information but
    it never returns you to a prompt. You have to hit the return key to
    get the shell prompt back.
    The same thing is happening to this program. Is there a way in perl
    to pass a return "\n" on the command to be executed that would return
    a prompt to the program so it does not hang? I have tried the
    following:
    snip
    It sounds like you need IPC::Open2* or IPC::Open3**. They allow you
    to run an external command and control its STDIN, STDOUT, and, in the
    case of IPC::Open3, STDERR. Try something like this:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use IPC::Open2;
    #if this is the child
    my $arg = shift;
    if (defined $arg and $arg eq 'you are the child') {
    print "I am the child\nwaiting for user to hit enter...\n";
    <STDIN>;
    exit;
    }
    #if this is the parent
    print "I am the parent, getting ready to run the child\n";
    #call myself with the argument "you are the child"
    open2 my $child_stdout, my $child_stdin, $0, "you are the child";
    print "child ran, sending return to it now\n";
    #send a return to the child to get it to quit
    print $child_stdin "\n";
    #read what it wrote to the stdout
    print "child said:\n", map { "\t$_" } <$child_stdout>;
    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
    Excellent !!! This looks like my answer! Will attempt to implement on
    09-Feb [Monday] and let you know.

    Thanks for your quick reply!
    A bit of a quagmire here. Seems open2 /open3 are not available to me
    at work.
    A little frustrated here. Are there any other alternatives?
  • Chas. Owens at Feb 10, 2009 at 2:14 pm
    On Tue, Feb 10, 2009 at 05:40, Ice Man wrote:
    snip
    A bit of a quagmire here. Seems open2 /open3 are not available to me
    at work.
    A little frustrated here. Are there any other alternatives?
    snip

    IPC::Open2 and IPC::Open3 are part of Core Perl and have been since
    the first version of Perl 5. If you are missing them then either you
    are using Perl 4 or earlier (incredibly unlikely) or someone has
    broken your Perl install (possible). More likely you are doing
    something wrong (like not including the modules with the use command).
    Try running

    perl -MIPC::Open2 -le 'print ok'

    on the command line. If it prints ok then you do in fact have the
    module. If gives you an error then please post the error and the
    results of this command

    perl -V

    If you do have the module, but are still having problems making it
    work, post sample code that demonstrates your problem and we will help
    you fix it. To get you started, here is a simple script that uses
    open2 to talk to the bc command:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use IPC::Open2;

    open2 my $out, my $in, "/usr/bin/bc";

    print $in "scale = 4\n1/2\nquit\n";

    print <$out>;


    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Rob Dixon at Feb 11, 2009 at 12:51 am

    Chas. Owens wrote:

    perl -MIPC::Open2 -le 'print ok'
    That will be

    perl -MIPC::Open2 -le 'print "ok"'

    Rob
  • Chas. Owens at Feb 11, 2009 at 2:17 am

    On Feb 10, 2009, at 19:51, Rob Dixon wrote:

    Chas. Owens wrote:
    perl -MIPC::Open2 -le 'print ok'
    That will be

    perl -MIPC::Open2 -le 'print "ok"'

    Rob
    While that will work, it isn't necessary. The strict pragma is not in
    force, so the bareword ok is the same as the string "ok" (since there
    are no functions or filehandles with that name in scope). I wouldn't
    advise doing it on a regular basis, but for something as simple as
    this, I believe it is fine. Most of the time I just say something like

    perl -MModule -e 0

    But I thought a bit of positive feedback would be useful here.
  • Chas. Owens at Feb 11, 2009 at 2:22 am

    On Tue, Feb 10, 2009 at 21:16, Chas. Owens wrote:
    On Feb 10, 2009, at 19:51, Rob Dixon wrote:

    Chas. Owens wrote:
    perl -MIPC::Open2 -le 'print ok'
    That will be

    perl -MIPC::Open2 -le 'print "ok"'

    Rob
    While that will work, it isn't necessary. The strict pragma is not in force,
    so the bareword ok is the same as the string "ok" (since there are no
    functions or filehandles with that name in scope). I wouldn't advise doing
    it on a regular basis, but for something as simple as this, I believe it is
    fine. Most of the time I just say something like

    perl -MModule -e 0

    But I thought a bit of positive feedback would be useful here.
    Drat, I am wrong. I could have sworn that worked, but print is
    forcing it to be treated as a file handle.


    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Bob McConnell at Feb 11, 2009 at 2:12 pm
    From: Rob Dixon
    Chas. Owens wrote:
    perl -MIPC::Open2 -le 'print ok'
    That will be

    perl -MIPC::Open2 -le 'print "ok"'
    Fascinating. I have been trying out Camelbox
    <http://code.google.com/p/camelbox/> on a new system with WinXP Pro SP2.
    When I paste that line into a console window, it gives me:

    -------8<-------------------------------------

    C:\Documents and Settings\rvm>perl -MIPC::Open2 -le 'print "ok"'
    Can't find string terminator "'" anywhere before EOF at -e line 1.

    C:\Documents and Settings\rvm>perl -Version
    Summary of my perl5 (revision 5 version 10 subversion 0) configuration:
    Platform:
    osname=MSWin32, osvers=5.1, archname=MSWin32-x86-multi-thread
    uname=''
    config_args='undef'
    hint=recommended, useposix=true, d_sigaction=undef
    useithreads=define, usemultiplicity=define
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=undef, use64bitall=undef, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    Compiler:
    cc='gcc', ccflags =' -s -O2 -DWIN32 -DHAVE_DES_FCRYPT
    -DPERL_IMPLICIT_CONTE
    XT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -DPERL_MSVCRT_READFIX',
    optimize='-s -O2',
    cppflags='-DWIN32'
    ccversion='', gccversion='3.4.5', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='long
    long', lseek
    size=8
    alignbytes=8, prototype=define
    Linker and Libraries:
    ld='g++', ldflags ='-s -L"C:\camelbox\lib\CORE" -L"C:\camelbox\lib"'
    libpth=C:\camelbox\lib
    libs=-lmsvcrt -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool
    -lcomdlg32 -
    ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr
    -lwinmm
    -lversion -lodbc32 -lodbccp32
    perllibs=-lmsvcrt -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool
    -lcomdlg
    32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32
    -lmpr -lwi
    nmm -lversion -lodbc32 -lodbccp32
    libc=-lmsvcrt, so=dll, useshrplib=true, libperl=libperl510.a
    gnulibc_version=''
    Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-mdll -s -L"C:\camelbox\lib\CORE"
    -L"C:\camelbox\
    lib"'


    Characteristics of this binary (from libperl):
    Compile-time options: MULTIPLICITY PERL_DONT_CREATE_GVSV
    PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS
    PERL_MALLOC_WRAP PL_OP_SLAB_ALLOC USE_ITHREADS
    USE_LARGE_FILES USE_PERLIO
    Built under MSWin32
    Compiled at Jul 18 2008 23:24:59
    @INC:
    C:/camelbox/lib
    C:/camelbox/site/lib
    .

    C:\Documents and Settings\rvm>

    -------8<-------------------------------------

    So now I wonder if Camelbox is broken.

    Bob McConnell
  • Rob Dixon at Feb 11, 2009 at 2:32 pm

    Bob McConnell wrote:
    From: Rob Dixon
    Chas. Owens wrote:
    perl -MIPC::Open2 -le 'print ok'
    That will be

    perl -MIPC::Open2 -le 'print "ok"'
    Fascinating. I have been trying out Camelbox
    <http://code.google.com/p/camelbox/> on a new system with WinXP Pro SP2.
    When I paste that line into a console window, it gives me:

    -------8<-------------------------------------

    C:\Documents and Settings\rvm>perl -MIPC::Open2 -le 'print "ok"'
    Can't find string terminator "'" anywhere before EOF at -e line 1.

    C:\Documents and Settings\rvm>perl -Version
    Summary of my perl5 (revision 5 version 10 subversion 0) configuration:
    Platform:
    osname=MSWin32, osvers=5.1, archname=MSWin32-x86-multi-thread
    uname=''
    config_args='undef'
    hint=recommended, useposix=true, d_sigaction=undef
    useithreads=define, usemultiplicity=define
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=undef, use64bitall=undef, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    Compiler:
    cc='gcc', ccflags =' -s -O2 -DWIN32 -DHAVE_DES_FCRYPT
    -DPERL_IMPLICIT_CONTE
    XT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -DPERL_MSVCRT_READFIX',
    optimize='-s -O2',
    cppflags='-DWIN32'
    ccversion='', gccversion='3.4.5', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='long
    long', lseek
    size=8
    alignbytes=8, prototype=define
    Linker and Libraries:
    ld='g++', ldflags ='-s -L"C:\camelbox\lib\CORE" -L"C:\camelbox\lib"'
    libpth=C:\camelbox\lib
    libs=-lmsvcrt -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool
    -lcomdlg32 -
    ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr
    -lwinmm
    -lversion -lodbc32 -lodbccp32
    perllibs=-lmsvcrt -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool
    -lcomdlg
    32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32
    -lmpr -lwi
    nmm -lversion -lodbc32 -lodbccp32
    libc=-lmsvcrt, so=dll, useshrplib=true, libperl=libperl510.a
    gnulibc_version=''
    Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-mdll -s -L"C:\camelbox\lib\CORE"
    -L"C:\camelbox\
    lib"'


    Characteristics of this binary (from libperl):
    Compile-time options: MULTIPLICITY PERL_DONT_CREATE_GVSV
    PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS
    PERL_MALLOC_WRAP PL_OP_SLAB_ALLOC USE_ITHREADS
    USE_LARGE_FILES USE_PERLIO
    Built under MSWin32
    Compiled at Jul 18 2008 23:24:59
    @INC:
    C:/camelbox/lib
    C:/camelbox/site/lib
    .

    C:\Documents and Settings\rvm>

    -------8<-------------------------------------

    So now I wonder if Camelbox is broken.
    This is one of the reasons why I deplore command-line Perl - because the format
    depends enormously on the command shell being used. The example was for a Unix
    shell, but with Windows command prompt only double-quotes will suffice to
    delimit a single parameter that contains space characters. So

    perl -MIPC::Open2 -le 'print "ok"'

    passes the string 'print to Perl, which then complains because the 'program' is
    malformed.

    With this restriction it is necessary to use the qq// form of double-quotes, so
    you would need to write

    perl -MIPC::Open2 -le "print qq/ok/"

    Camelbox is (probably) fine :)

    HTH,

    Rob
  • Chas. Owens at Feb 11, 2009 at 2:42 pm
    On Wed, Feb 11, 2009 at 09:31, Rob Dixon wrote:
    snip
    This is one of the reasons why I deplore command-line Perl - because the format
    depends enormously on the command shell being used. The example was for a Unix
    shell, but with Windows command prompt only double-quotes will suffice to
    delimit a single parameter that contains space characters.
    snip

    This is one of the reasons I deplore people using cmd.exe. If you use
    bash on Windows you have no problems. That is why installing Cygwin
    is second only to installing Firefox for me when setting up a new
    Windows box.


    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 6, '09 at 7:34p
activeFeb 11, '09 at 2:42p
posts12
users5
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase