New to Perl - strange behaviour with backticks

Discussion in 'Perl Misc' started by Eric, Sep 13, 2007.

  1. Eric

    Eric Guest

    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
     
    Eric, Sep 13, 2007
    #1
    1. Advertising

  2. 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);

    <code snipped>

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 13, 2007
    #2
    1. Advertising

  3. Eric

    Guest

    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.
     
    , Sep 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristina Clair
    Replies:
    0
    Views:
    694
    Kristina Clair
    Aug 27, 2004
  2. Steven Brent

    Backticks: What up?

    Steven Brent, Apr 28, 2004, in forum: Python
    Replies:
    6
    Views:
    351
    Steve Holden
    Apr 28, 2004
  3. Sam Stephenson
    Replies:
    9
    Views:
    251
    Caleb Clausen
    Nov 7, 2005
  4. Chris McMahon
    Replies:
    2
    Views:
    221
    David Vallner
    Oct 19, 2006
  5. Giles Bowkett

    backticks asynchronous?

    Giles Bowkett, Nov 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    93
    Giles Bowkett
    Nov 20, 2006
Loading...

Share This Page