2

I am working on CnvPytor and trying to automate some commands using Perl. But it is not working because when I give first command it is going inside another bash prompt (cnvpytor). Please refer the below screenshot,

enter image description here

I have written the below script, but it is not working

$x = `cnvpytor -root s1.pytor -view 10000`;     #normal bash
$x = `set Q0_range -1 0.5`;                     # this and after this, enter into the cnvpytor
$x = `set p_range 0 0.0001`;
$x = `set p_N 0 0.5`;
$x = `set print_filename s1_output.vcf`;
$x = `set annotate`;
$x = `print calls`;
$x = `quit`;
2

3 Answers 3

2

If you don't need the output from the command, use:

open(PIPE, '|cnvpytor -root s1.pytor -view 10000');
print PIPE <<'EOF';
set Q0_range -1 0.5
set p_range 0 0.0001
set p_N 0 0.5
set print_filename s1_output.vcf
set annotate
print calls
quit
EOF
close(PIPE);

If you do need the output to the command, you would write your cnvpytor commands to a file and then do:

open(INPUT, 'cnvpytor -root s1.pytor -view <filename |');
while (<INPUT>) {
     # Do something with each line of output
}
close(INPUT);
unlink('filename');

Where "filename" is your temporary file with input.

Sign up to request clarification or add additional context in comments.

3 Comments

Re "you would write your cnvpytor commands to a file", Well, I would use IPC::Run
IPC::Run is a fine module (and I might use it for the second example too), but it is more complicated.
That's the point. The complicated stuff is in the module, instead of forcing you to do it. Even your half-assed method of using a shell and a temporary file requires a dozen lines of code -- something you found so long that you only include half of it in your answer -- whereas it would be one line with IPC::Run. It's also far more reliable: You can get better error reporting. You don't have to worry about code injection, you don't need to permission to write to disk. You don't have to worrk about disk space. There's no possibility of a temporary files being left behind. And so on.
1

There's no need for a temporary file, and you can still do it using backticks if you don't mind it using a shell. This also will allow you to capture the output of the cnvpytor command, if there is any. Here's how I would do it:

my $output = `cnvpytor -root s1.pytor -view 10000 2>&1 <<EOT
set Q0_range -1 0.5
set p_range 0 0.0001
set p_N 0 0.5
set print_filename s1_output.vcf
set annotate
print calls
quit
EOT`;

The 2>&1 redirects STDERR to STDOUT, which will cause it to be captured in $output. I feel that's usually a good idea whenever using backticks.

Comments

0

Each backtick pair runs a separate process.

I have no idea what CnvPytor is (and the link you provided doesn't help much), but the usual way is to run the external tool using some kind of IPC to communicate with it.

2 Comments

Thanks for you answer :-) I got you point, if I make it as each backtick it runs as separate process. But I merged all command as single command with ; separated and tried it in a single backtick but still I am not getting the result.
; separates shell commands. You want to run a single command, cnvpytor, and send the rest as input to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.