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

Dear monks, I am using php for making an webserver. since i know a little of perl compared to PHP, the values which is on the php page is passed to a perl program. To make things easier, i am using unix system command. For example, to search an input ID, i use unix grep command. Since, i also need the sequence of the given ID, my program looks like this.
file name1:seq.txt ------------------- >ID_101 AGCTGAGTAGACTGA >ID_102 CGATGCTAGCTAGCTACAT >ID_103 ATCTTATCTTTTTATACC file name2:seq_updated.txt -------------------------- >ID_104 AATAGCTAGAAGGA >ID_105 AGATCGACTGAA >ID_106 ACTAGCTACGATATATTATATATAT my php page: ----------- $foo="ID_106" //ID to be searched. $cmd="perl split.pl $foo"; echo exec($cmd); my perl program: --------------- $bar=$ARGV[0]; print exec("grep -h -A 1 $bar *.txt");
My doubts: Why am i not able to store the exec command in a scalar or array variable and then print it. It doesnt work that way, and it works only if i print it directly. I need to save it to a variable because, i need to split the sequence line into 5 characters per line.
what i was planning to do with the variable is this: ---------------------------------------------------- foreach $str(@array){ if($str=~/\w+/){@strs=split(//,$str); for($i=1;$i<=scalar(@strs);$i++){ print "$strs[$i] "; if($i%5==0){print"\n";} } } } } this is how my webpage result should look like: ----------------------------------------------- >ID_106 ACTAG CTACG ATATA TTATA TATAT
well, am i making things complicated? Can i use unix one liner system command to print 5 characters per line when i grep the ID? Please guide. Thank you.

Replies are listed 'Best First'.
Re: system command and formatting
by lakshmananindia (Chaplain) on Feb 27, 2009 at 04:58 UTC
    I need to save it to a variable

    Use backtick `` to execute the command and store the result in a variable.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: system command and formatting
by irah (Pilgrim) on Feb 27, 2009 at 04:47 UTC

    When come to execution of exec function, it will replace the code or program. The exec function won't return back to the stage where it started. So it is better, use system function. Further more information read man 3 exec