New to Perl - strange behaviour with backticks

New to Perl - strange behaviour with backticks

am 13.09.2007 23:08:47 von venner

I am trying to set up a CGI script that will run two other scripts and
then write a short message back to the webpage. I have this mostly
working, except for one strange issue: after using backticks to read
the stdout from the first script, stdout from the second script is
being printed to the page as if it were printed from the CGI script
that acts as the caller. The behaviour disappears if I comment out
the backticks, but then I can't get the information i need from the
stdout of the first script. The code follows:

#!/usr/bin/perl
use CGI;

print "Content-type: text/html\n\n";
my $query = new CGI;
my $pdbCode = $query->param("pdbcode");
my $scriptPath = "~/Sites/etaserver/bin";
my $rankPath = "~/Documents/Templates/Distance\ Matcher/Data/2006/
Data";
my $pdbPath = "~/Documents/Templates/Distance\ Matcher/Data/
2006/2006pdb90\ ET/$pdbCode";
my $pymolPath = "/sw/lib/pymol-py24/bin/pymol";

my $extractMotifArgs = "perl -I$scriptPath \"$scriptPath/
extractmotif.pl\" \"$pdbPath/$pdbCode.clusters\" \"$pdbPath/
$pdbCode.dssp\" \"$pdbPath/$pdbCode.pdb\" \"$pdbPath/$p
my $motifs = `$extractMotifArgs`;
#my $motifs="1,2,3,4";

if($pdbCode =~ /^\d\w{3}\w?$/ && -e $pdbPath) {
$pdbPath =~ s/\\//g;
my $args = ("$pymolPath -cr $scriptPath/createPyMolImage.py --
$pdbCode \"$pdbPath\" $motifs");
system($args);
if (($? >> 8) == 0) {
print "CreatedImage:".$pdbCode;
} else {
print "Error:Unable to Create image";
}
} else {
print "Error:Unable to find PDB Data\n";
}

This is Perl 5.8.6.

Thank you,
Eric

Re: New to Perl - strange behaviour with backticks

am 13.09.2007 23:46:34 von Gunnar Hjalmarsson

Eric wrote:
> I am trying to set up a CGI script that will run two other scripts and
> then write a short message back to the webpage. I have this mostly
> working, except for one strange issue: after using backticks to read
> the stdout from the first script, stdout from the second script is
> being printed to the page as if it were printed from the CGI script
> that acts as the caller. The behaviour disappears if I comment out
> the backticks, but then I can't get the information i need from the
> stdout of the first script.

Assuming that

system($args);

executes the second script you are talking about, what if you call also
that script using backticks (in void context):

qx($args);



--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: New to Perl - strange behaviour with backticks

am 14.09.2007 00:02:01 von xhoster

Eric wrote:
> I am trying to set up a CGI script that will run two other scripts and
> then write a short message back to the webpage. I have this mostly
> working, except for one strange issue: after using backticks to read
> the stdout from the first script, stdout from the second script is
> being printed to the page as if it were printed from the CGI script
> that acts as the caller.

The second time you invoke an external command, you do it with
"system". That is the way system works--it writes its output to the
stdout it inherits from Perl.

> The behaviour disappears if I comment out
> the backticks,

That seems unlikely. Perhaps when you comment out the backticks, the
system($args) no longer produces any output at all because it is not
passed anything interesting as its argument.


> #!/usr/bin/perl
> use CGI;
>
> print "Content-type: text/html\n\n";
> my $query = new CGI;
> my $pdbCode = $query->param("pdbcode");
> my $scriptPath = "~/Sites/etaserver/bin";
> my $rankPath = "~/Documents/Templates/Distance\ Matcher/Data/2006/
> Data";
> my $pdbPath = "~/Documents/Templates/Distance\ Matcher/Data/
> 2006/2006pdb90\ ET/$pdbCode";
> my $pymolPath = "/sw/lib/pymol-py24/bin/pymol";
>
> my $extractMotifArgs = "perl -I$scriptPath \"$scriptPath/
> extractmotif.pl\" \"$pdbPath/$pdbCode.clusters\" \"$pdbPath/
> $pdbCode.dssp\" \"$pdbPath/$pdbCode.pdb\" \"$pdbPath/$p
> my $motifs = `$extractMotifArgs`;
> #my $motifs="1,2,3,4";

Both for purposes of posting here, and for purposes of your own debugging,
you should probably replace the external commands with calls to
simple commonly available system utilities, like echo or cat. That will
help you isolate the problem to the perl-specific part versus the
external part, and help us help you because we would be able to test
your code ourselves.

my $motifs=`echo 1,2,3,4`;


Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Re: New to Perl - strange behaviour with backticks

am 14.09.2007 07:44:32 von Joe Smith

Eric wrote:
> I am trying to set up a CGI script that will run two other scripts and
> then write a short message back to the webpage. I have this mostly
> working, except for one strange issue: after using backticks to read
> the stdout from the first script, stdout from the second script is
> being printed to the page as if it were printed from the CGI script
> that acts as the caller.

If the first script is called using backticks and the second one is
called using system(), then that is proper and expected behavior.

> The behaviour disappears if I comment out
> the backticks, but then I can't get the information i need from the
> stdout of the first script.

That does not sound right - could be that the second script is not
sending anything at all to stdout.

> print "Content-type: text/html\n\n";
> my $query = new CGI;

You could use methods in the CGI module to output the Content-type.

> my $pdbCode = $query->param("pdbcode");
> my $scriptPath = "~/Sites/etaserver/bin";
> my $rankPath = "~/Documents/Templates/Distance\ Matcher/Data/2006/
> Data";
> my $pdbPath = "~/Documents/Templates/Distance\ Matcher/Data/
> 2006/2006pdb90\ ET/$pdbCode";
> my $pymolPath = "/sw/lib/pymol-py24/bin/pymol";
>
> my $extractMotifArgs = "perl -I$scriptPath \"$scriptPath/
> extractmotif.pl\" \"$pdbPath/$pdbCode.clusters\" \"$pdbPath/
> $pdbCode.dssp\" \"$pdbPath/$pdbCode.pdb\" \"$pdbPath/$p

Have you heard of qq()? It eliminates the need to escape double quotes.

my $extractMotifArgs = qq(perl -I$scriptPath "$scriptPath/extractmotif.pl" "$pdbPath/$pdbCode.clusters" "$pdbPath/$pdbCode.dssp" "$pdbPath/$pdbCode.pdb" "$pdbPath/$p");

> my $args = ("$pymolPath -cr $scriptPath/createPyMolImage.py --
> $pdbCode \"$pdbPath\" $motifs");
> system($args);

my $args = qq($pymolPath -cr $scriptPath/createPyMolImage.py -- $pdbCode "$pdbPath" $motifs);
my $result = `$args print "Error: Unexpected output from $pymolPath: '$result'\n";

-Joe