P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

Brothers and Sisters of this fine Monastery,

I recently coded up some Java classes to extend the functionality of the Task class within Ant. One of the tasks is a Perl script which allows for command line options. I created the execute() method within my task to call the Perl script. Since I am not done completely coding all the methods in the class yet, I am directly passing in the "-h" command line option to invoke help. Here is the code:

public void execute() throws BuildException { Commandline commandLine = new Commandline(); Project aProj = getProject(); int result = 0; // build the command line from what we got the format is // perl foo.pl [options...] commandLine.setExecutable(getFooCommand()); commandLine.createArgument().setValue(COMMAND_FOO); commandLine.createArgument().setValue("-h"); // Check the command line options checkOptions(commandLine); // For debugging System.out.println(commandLine.toString()); result = run(commandLine); if ( result != 0 ) { String msg = "Failed executing: " + commandLine.toString() +; throw new BuildException(msg, location); } }

When the script executes, none of the output from the Perl script is appearing on the screen. If I invoke the script from the command line:

C:\>perl foo.pl -h

it works fine and the output displays to the screen.

Does anyone know where my STDOUT went?

Thank you :)

P0w3rK!d

Replies are listed 'Best First'.
Re: Ant, Perl, and where did my STDOUT go?
by Abigail-II (Bishop) on Jun 04, 2002 at 15:01 UTC
    This looks like a Java question to me. There's no Perl in here, so it's a bit hard to say (perhaps the Perl program closes STDOUT, who knows?). But it's easy to check whether it's a Java question or not. Replace the Perl command with something else that writes to STDOUT. If that output also disappears, you'd have to dive in your Java manuals.

    Abigail

      I executed the following by itself:
      commandLine.setExecutable(getPerlCommand()); commandLine.createArgument().setValue("-h");
      and it worked fine:
      C:\ant\bin>ant
      Buildfile: build.xml
      
      main:
      foo c:\perl\bin\perl.exe -h
      
      foo Usage: c:\Perl\bin\perl.exe switches -- programfile arguments
      foo   -0octal       specify record separator (\0, if no argument)
      foo   -a              autosplit mode with -n or -p (splits $_ into @F)
      foo   -c              check syntax only (runs BEGIN and END blocks)
      foo   -d:debugger   run scripts under debugger
      foo   -Dnumber/list set debugging flags (argument is a bit mask or flags)
      foo   -e 'command'    one line of script. Several -e's allowed. Omit programfile.
      foo   -F/pattern/     split() pattern for autosplit (-a). The //'s are optional.
      foo   -iextension   edit <> files in place (make backup if extension supplied)
      foo   -Idirectory     specify @INC/#include directory (may be used more than once)
      foo   -loctal       enable line ending processing, specifies line terminator
      foo   -mM-module.. executes `use/no module...' before executing your script.
      foo   -n              assume 'while (<>) { ... }' loop around your script
      foo   -p              assume loop like -n but print line also like sed
      foo   -P              run script through C preprocessor before compilation
      foo   -s              enable some switch parsing for switches after script name
      foo   -S              look for the script using PATH environment variable
      foo   -T              turn on tainting checks
      foo   -u              dump core after parsing script
      foo   -U              allow unsafe operations
      foo   -v              print version number, patchlevel plus VERY IMPORTANT perl info
      foo   -V:variable   print perl configuration information
      foo   -w              TURN WARNINGS ON FOR COMPILATION OF YOUR SCRIPT. Recommended.
      foo   -xdirectory   strip off text before #!perl line and perhaps cd to directory
      foo
      

      The question now would be, how do I encapsulate the script and *its* command line variables and call it with Perl? Otherwise, it appears Perl may not be executing the script at all...

      Thank you :>

      -P0w3rK!d

Re: Ant, Perl, and where did my STDOUT go?
by lachoy (Parson) on Jun 04, 2002 at 15:20 UTC

    I don't know if this is a Win32 thing, because the following works for me:

    Buildfile build-perl.xml:

    <project name="mytest" default="run_perl" basedir="."> <target name="run_perl" description="Test STDOUT"> <exec executable="perl" dir="${basedir}"> <arg value="foo.pl"/> <arg value="-h yes"/> </exec> </target> </project>

    Script: foo.pl

    #!/usr/bin/perl use strict; use Getopt::Std; my %opts = (); getopt( 'h', \%opts ); if ( $opts{h} ) { print "Printing help..."; } else { print "No help requested, none given."; }

    Running it gives me:

    [wintercm@juvenile test]$ ant -buildfile build-perl.xml Buildfile: build-perl.xml run_perl: [exec] Printing help... BUILD SUCCESSFUL Total time: 2 seconds

    And if I comment out the second argument from the exec task, I get:

    [wintercm@juvenile test]$ ant -buildfile build-perl.xml Buildfile: build-perl.xml run_perl: [exec] No help requested, none given. BUILD SUCCESSFUL Total time: 2 seconds

    ...which is exactly what I'd expect. Try running this on your machine to see what happens. If it doesn't work, look into the docs to see if Ant on Win32 does anything funky with STDOUT.

    Chris
    M-x auto-bs-mode

Re: Ant, Perl, and where did my STDOUT go? (Correction)
by P0w3rK!d (Pilgrim) on Jun 04, 2002 at 15:01 UTC
    commandLine.setExecutable(getFooCommand());
    Should be:
    commandLine.setExecutable(getPerlCommand());
    :)