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

Hello Folks, I have been trying to run a perl script to generate the output in .txt file. I am actually executing a sqlplus procedure using system command to write output in a .txt file. The sqlplus gives output in newline separated form, but after executing the system command, it is somehow truncating the trailing newlines from everyline, effectively writing the output to a single large line. The code I am trying to execute is: $cmd = "echo 'exec javasplat.create_test_list(\'tests.txt\', \'$startStr\', \'$endStr\');' | $sqlplusconnect" ; system($cmd); I am doing the above thing in bash environment. After going through few reviews, I came to conclusion that bash command substitution is removing trailing newlines. I tried setting the $IFS variable to empty string, it didn't work as it wasn't there at first place. Can you guys suggest me how do I prevent my script from truncating newline characters? Thanks, Harshit
  • Comment on PERL: newlines truncated from sqlplus output in perl

Replies are listed 'Best First'.
Re: newlines truncated from sqlplus output in perl
by haukex (Archbishop) on Jun 29, 2018 at 08:21 UTC
    $cmd = "echo 'exec javasplat.create_test_list(\'tests.txt\', \'$startStr\', \'$endStr\');' | $sqlplusconnect" ; system($cmd);

    You haven't shown us the contents of those variables, so it's very hard to say what's going on. Please show a more representative code sample - see Short, Self-Contained, Correct Example.

    In general: You're interpolating strings into the command to be executed, which is a potential security risk, also you have to be sure you're getting the quoting right. You're also using the shell to feed input into the command's STDIN. Both of these issues can be solved by using a module and avoiding the shell in the first place - I wrote about this topic at length here. Here's an example using IPC::Run3, which has the advantage of also being able to capture STDOUT and STDERR, if you like. Note that you still need to make sure that $startStr and $endStr are quoted properly, but you don't need to worry about escaping shell metacharacters anymore.

    use warnings; use strict; use IPC::Run3; my $stdin = q{exec javasplat.create_test_list('tests.txt',} .qq{ '$startStr', '$endStr');}; run3 ['sqlplus','-s','username/password@host'], \$stdin or die "run3 failed"; $? and die "external command failed, \$?=$?";
    it is somehow truncating the trailing newlines from everyline, effectively writing the output to a single large line

    How did you determine this? I would recommend looking at the file with a command like hexdump -C filename; maybe it's just a CR/LF problem.

    I am doing the above thing in bash environment

    Note that system calls the default shell at /bin/sh, which is not necessarily bash - see The problem of "the" default shell.

Re: PERL: newlines truncated from sqlplus output in perl
by marto (Cardinal) on Jun 29, 2018 at 09:41 UTC

    In addition to what haukex said, is there a reason you're not doing everything from one perl script? Work with Oracle in a sane manner with DBI/DBD::Oracle.