henrycui has asked for the wisdom of the Perl Monks concerning the following question:

I have a Java application, which invokes perl. Here is my java code:
Process p = Runtime.getRuntime().exec("c:/perl.exe" C:/CreateER.pl "Please \n ignore \n my friend");
This invokes the perl from Java. In my perl script, I get the argument in this way:
@ARGV[1] =~ s/\\n/\n/g; print(@ARGV[1]);
The line break was removed when retrieving the argument. The argument becomes:
Please ignore my friend
I want to keep the line breaks. This is not what I want. However, if I run the perl command directly:
c:/perl.exe C:/scripts2/CreateER.pl "Please \n ignore \n my friend");
It works fine. The correct argument is retrieved:
Please ignore my friend
So it appears the problem happens in the Perl/Java integration. Any ideas? Thanks very much!

Replies are listed 'Best First'.
Re: Problem with the Perl/Java intergarion when new line break is used in the argument
by pobocks (Chaplain) on Dec 12, 2008 at 17:31 UTC
    Just for curiosity's sake, try:
    Process p = Runtime.getRuntime().exec( "c:/perl.exe" C:/CreateER.pl "Please \\n ignore \\n my friend");

    Memory and cursory googling point toward backslash being an escape character in Java, as well as Perl... Thus, your perl program is getting those as literal line breaks.

    Essentially, it's being called as:

    c:/perl.exe C:/CreateER.pl "Please ignore my friend"

    Update: I tested my theory in Eclipse. A: I was correct. B: You totally owe me a coke for making me have to deal with eclipse ;-)

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
      I do owe you a coke. You solved my problem. Thanks very much!

        Glad to help ;-)

        Escaping mechanisms have been the bane of my existence often enough; especially Javascript/(PHP|Perl)/HTML conjoint escape issues. ::shudder::

        for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
Re: Problem with the Perl/Java intergarion when new line break is used in the argument
by toolic (Bishop) on Dec 12, 2008 at 17:13 UTC
    I also know nothing about Java. But, it looks like the Java "exec" is not treating the "\n" as a newline character. Or maybe the double quotes in Java are performing some type of interpolation. Does Java have any non-interpolation quotes, like single quotes (q) in Perl?

    As a (lame) workaround, since you are performing a substitution on "\n" in your perl code, maybe you could use a different delimiter, like ":"

    Process p = Runtime.getRuntime().exec("c:/perl.exe" C:/CreateER.pl "P +lease : ignore : my friend");

    then:

    s/:/\n/g;

    Update: As an aside, your code spews a warning if you add use warnings; (verbose explanation provided by use diagnostics;):

    Scalar value @ARGV[1] better written as $ARGV[1] at ... (W syntax) You've used an array slice (indicated by @) to select a single element of an array. Generally it's better to ask for a sc +alar value (indicated by $). The difference is that $foo[&bar] always behaves like a scalar, both when assigning to it and when evaluati +ng its argument, while @foo[&bar] behaves like a list when you assign to +it, and provides a list context to its subscript, which can do weird t +hings if you're expecting only one subscript. On the other hand, if you were actually hoping to treat the array element as a list, you need to look into how references work, beca +use Perl will not magically convert between scalars and lists for you. + See perlref.
Re: Problem with the Perl/Java intergarion when new line break is used in the argument
by matze77 (Friar) on Dec 12, 2008 at 16:48 UTC

    Hmm. Without knowing Java is the "\n" maybe ignored (what is the newline command looking like in Java) in transformation? (You need something like carriage return, line feed ...?) Just a thought ...

Re: Problem with the Perl/Java intergarion when new line break is used in the argument
by Anonymous Monk on Dec 12, 2008 at 17:55 UTC
    Your java code, as it appears in your question:
    Process p = Runtime.getRuntime().exec("c:/perl.exe" C:/CreateER.pl "Please \n ignore \n my friend");
    This is not valid java code. Please edit. Please consider using the preview button.