in reply to How to keep the line breaks in the arguments when I invoke Perl?

Have you tried this:
my_script 'my\narg\nwith\nline\nbreaks' ... $ARGV[0] =~ s/\\n/\n/g; ...
(update: actually, you could use any distinctive character or pattern of your choosing to serve as a placeholder for line breaks -- just be clear about it in your "Usage: ..." synopsis and/or man page, and do the appropriate regex replacement as the first thing in the script.)
  • Comment on Re: How to keep the line breaks in the arguments when I invoke Perl?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to keep the line breaks in the arguments when I invoke Perl?
by henrycui (Initiate) on Dec 12, 2008 at 15:38 UTC
    Thanks for all the replies. Let me explain my scenario in detail. 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
    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!
      I'm no java whiz, so maybe that's why I find your java syntax to be pretty confusing:
      Process p = Runtime.getRuntime().exec("c:/perl.exe" C:/CreateER.pl "Please \n ignore \n my friend");
      Why no comma after "c:/perl.exe", or after C:/CreateER.pl? For that matter, why does the java code use C:/CreateER.perl, whereas your working shell command ("running it directly") has C:/scripts2/CreateER.pl ?

      I gather that java is interpreting the "\n" in the last arg, and outputting actual line-feed characters in their place, which I would expect to make a mess of some sort.

      I don't know if this is doable for you, but the next thing I would try would be to open the perl process as a pipeline file handle, and print to that file handle the string that contains any given number of line-feed characters, so that perl script reads this from its STDIN (rather than getting it in @ARGV).

      I don't know how that would look in java (or whether you can do it at all) -- it would be done like this in perl:

      my $pid = open( PROC, "|-", "c:/perl.exe c:/scripts2/CreateER.pl" ) or die "cannot launch perl process: $!"; print PROC "anything\nyou\nwant\nto\ndo\n"; close PROC;
      Another possibility: print the multi-line string in question to a file (surely java can do that), and then have the "exec()" call run the perl script with the name of the file, so perl can read the lines with while(<>) or whatever.

      Or, maybe try just making the string look like this in java:

      Please \\n ignore \\n my friend
      So that the string ends up being a single line with literal "backslash""n" sequences in it, which perl can then interpret as intended.

      UPDATE: It sounds like you haven't tried one of the other ideas in my initial reply: before passing your multi-line string to the exec call as a command-line arg for your perl script, convert the "\n" characters (or literal "backslash""n" sequences) to something safe and innocuous (like "=:=" or anything that won't ever be confused with other stuff in the string), and modify the perl script so it knows to convert this back to "\n" when it gets the string out of @ARGV.