Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Running Perl program using backquotes

by nysus (Parson)
on Apr 18, 2001 at 01:52 UTC ( [id://73362]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to call "Program B.cgi" from "Program A.cgi". Program B expects input from the command line. I'm trying to get Program A to act like a command line call for Program B. I try the following line in Program A:

`perl Program B.cgi filetoprocess > outputfile`;
Using the backquotes here works great. It processes the input file and outputs it to another.

However, when I throw in a switch to pass to Program B, like so:

`perl Program B.cgi -t filetoprocess > outputfile`;
what happens is that Program A halts execution and waits for me to provide input. After entering some input, Program B processes the input I provide and not the contents of "filetoprocess".

Can anyone please give me a clue?

Replies are listed 'Best First'.
Re: Running Perl program using backquotes
by sachmet (Scribe) on Apr 18, 2001 at 02:04 UTC
    <chant>without code samples it's hard to say </chant>

    That said, Program B likely expects an argument to the -t switch, and is using filetoprocess for that. Check that -t requires no additional arguments that would gobble up the filetoprocess argument.

    The described behavior would happen if you had a <> in your program somewhere, and in ProgramB.cgi you were shifting off two elements of @ARGV based on the -t switch.
      Brother, thanks for your patience...this was exactly what was happening. Slowly but surely, I'll get all this figured out.

      $PM = "Perl Monks'";
      $MCF = "Most Clueless Friar";
      $nysus = $PM . $MCF;

Re: Running Perl program using backquotes
by mothra (Hermit) on Apr 18, 2001 at 03:20 UTC
    Also keep in mind that backticks are only used when you want to capture the output of the program you run within them.

    From perlfaq8:

    What's wrong with using backticks in a void context?

    Strictly speaking, nothing. Stylistically speaking, it's not a good way to write maintainable code because backticks have a (potentially humongous) return value, and you're ignoring it. It's may also not be very efficient, because you have to read in all the lines of output, allocate memory for them, and then throw it away.

    Note that backticks return whatever the program that's run with them sends to STDOUT. Considering that you're redirecting STDOUT to a file here, you're probably better off using system.

      More food for thought...thanks. I will definitely read up on these funky functions.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar;
      $nysus = $PM . $MCF;

Re: Running Perl program using backquotes
by Beatnik (Parson) on Apr 18, 2001 at 12:16 UTC
    <RANT> cgi isnt de facto perl and vice versa... I doubt your CGI script generates proper HTTP headers, and if it does... WHY??</RANT>

    If you want to call CGI scripts from other CGI scripts, use LWP

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      Maybe I will when I figure out how the LWP module works. Being new to this, I'm not so interested in the best or most correct solution as I am in getting the damn thing to work in the first place. :-)

      But thanks for the advice.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar;
      $nysus = $PM . $MCF;

        For plain GET calls, you can use LWP::Simple (if you dont need any specific referer, or cookie stuff)
        #!/usr/bin/perl -w use strict; use LWP::Simple; my $result = get(http://www.perlmonks.com/index.pl?node_id=131");
        With POST you'll need
        #!/usr/bin/perl -w use strict; use LWP::UserAgent; use URI::URL; my $WWWAgent = new LWP::UserAgent(); my $url=new URI::URL 'http://www.perlmonks.org/index.pl'; $url->query_form(node_id => "131"); my $WWWRequest = new HTTP::Request 'POST', $url->as_string() ; my $WWWResult = $WWWAgent->request($WWWRequest); die "Error logging in $WWWResult->code $WWWResult->message" if(!$WWWRe +sult->is_success); print $WWWResult->content;
        Greetz
        Beatnik
        ... Quidquid perl dictum sit, altum viditur.
Re: Running Perl program using backquotes
by Anonymous Monk on Apr 18, 2001 at 21:41 UTC
    try putting a \ in front of the hyphen. It may be a special character that is being read wrong by perl. The \ will make perl read it literally.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://73362]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-29 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found