Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Running an external command

by Eldan Aranye (Acolyte)
on Apr 26, 2012 at 03:41 UTC ( #967224=perlquestion: print w/replies, xml ) Need Help??
Eldan Aranye has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, In relation to my other question (the one with redirecting STDERR), I would like to ask this: what if the command outputs a file? How can I store the data using Perl? Will doing this:

@output = `/home/.../samtools view...`

be enough? Or am I missing something? Using the console, I am able to do this by doing:

/home/.../samtools view ... > output.bam

I hope I was able to deliver this clearly. Thanks in advance for any help!


Replies are listed 'Best First'.
Re: Running an external command
by davido (Archbishop) on Apr 26, 2012 at 04:31 UTC

    I haven't read your other question. But you're about half-way there. If @output holds the data that you want saved, you next need to do something like this:

    open my $out_fh, '>', 'filename.txt' or die $!; print $out_fh @output; close $out_fh or die $!;

    That's a simple means. But you'll probably need to modify it to better suit your needs, which weren't really fully described in this thread.

    perlintro will take about a half hour to read. One of the sections in that document contains a brief introduction to file input and output.


Re: Running an external command
by 2teez (Vicar) on Apr 26, 2012 at 04:29 UTC

    If your program output a file, why use a backtick? simply use a open function in Perl, like this:

    use warnings; use strict; my $output_file="output.bam"; open my $fh,'>',$output_file or die "can't open $output_file:$!"; print $fh " ... hello world into my file..."; close $fh or die "can close file: $!";
    Please, check perldoc perlopentut
    or perldoc -f open for more ...
    Hope this helps

Re: Running an external command
by GrandFather (Sage) on Apr 26, 2012 at 04:26 UTC

    What did you try and how did it fail to meet your expectations?

    True laziness is hard work
Re: Running an external command
by icoder (Novice) on Apr 26, 2012 at 08:19 UTC
    As all suggested,Read the file and put that contents into buffer
    open FILE,"< generatedFile.txt" or die "cant able to read file


    @output = `/home/.../samtools view...` @fileout = `cat file.txt 2>&1`;

    This works only in unix

Re: Running an external command
by Eldan Aranye (Acolyte) on Apr 26, 2012 at 09:11 UTC

    Actually, I ran this:

    open INP,"< $ARGV[0]" or die "Cannot open file: $!"; $file = "$ARGV[1]"; open OUT, "> $file" or die "Cannot open file: $!"; @out = `/home/applications/samtools-0.1.7a/samtools view -bS $ARGV[0] +2>&1`; print OUT @out; close INP; close OUT;

    And I was able to produce a non-empty file. Unfortunately, it got truncated somehow. Is it because of my use of backticks?

      If the file referred to by $ARGV[1] should be appended to instead of overwritten then use the following code. Since you aren't actually using INP, I've removed it.

      $file = "$ARGV[1]"; open OUT, ">> $file" or die "Cannot open file: $!"; @out = `/home/applications/samtools-0.1.7a/samtools view -bS $ARGV[0] +2>&1`; print OUT @out; close OUT;

      A better way to write that is shown below. The top three lines assume that this is a standalone script which it may not be.

      #!/usr/bin/perl use strict; use warnings; use autodie; my $file = $ARGV[1]; open my $out, '>>', $file; my @out = `/home/applications/samtools-0.1.7a/samtools view -bS $ARGV[ +0] 2>&1`; print $out @out; close $out;
      Almost certainly, no.

      But I, for one, need a more precise description of the problem than "...truncated somehow."

Re: Running an external command
by Eldan Aranye (Acolyte) on Apr 30, 2012 at 00:34 UTC
    I'll try running the code with the error and see if I come up with that truncated error message I got last week. Also, thanks for the tips! I'll be trying them out and see if my problem would be solved. Thanks again!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967224]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-24 13:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.