Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Can I call a CGI from another CGI?

by ArmandoG (Sexton)
on Dec 27, 2007 at 23:51 UTC ( [id://659272]=perlquestion: print w/replies, xml ) Need Help??

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

Hi I am trying to do these
use strict; use CGI; my $list = new CGI; my $fileDir = "c:/files/err"; my @files; opendir DIR, "$fileDir" or die "Can't open $fileDir $!"; @files = grep { /\.(?:txt|zip|jpg|mp3|err|csv)$/i } readdir DIR; closedir DIR; print $list->header("text/html"), $list->start_html("Archivos in $fileDir"), $list->p("Estos son los archivos de $fileDir"); foreach my $file (@files) { # print $list->p($list->a({-href=>$fileDir."/".$file},$file) print $list->p($list->a({-href=>"/cgi-bin/download.cgi?ID=$file">" +$file"}) #"/archive/".$file},$file) ); } print $list->end_html;
this script calls a cgi script call "download.cgi" can I do that? and can you take a look at the code for errors thanks

Replies are listed 'Best First'.
Re: Can I call a CGI from another CGI?
by eserte (Deacon) on Dec 27, 2007 at 23:54 UTC
    You have some options. If the CGI script lives on the same machine, then you can call it directly using system() or `...`. It would be even nicer if the script could be refactored into a module/script combo, then you can "use" it and just call functions or methods. If the CGI is on another host or must be called via HTTP, then you can use LWP::UserAgent.
Re: Can I call a CGI from another CGI?
by bradcathey (Prior) on Dec 28, 2007 at 01:24 UTC

    I totally agree with eserte and think you should just roll a method to do this instead of calling another script.

    I know the temptation. I used to try to make something like this work as a option in my early CGI days, but then I started calling "libraries" of common routines in module form. And from the sound of it, something like a download routine could be called in a variety of situations. When you start doing that, you need to seriously think the module route. Besides, when you start doing system calls, things can get dicey.

    Take a look at CGI::Application and some of its easy-to-use redirects and instances.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Can I call a CGI from another CGI?
by NetWallah (Canon) on Dec 28, 2007 at 04:13 UTC
    It appears that you are attempting to follow my response to your previous question Getting the link to work.

    Again, phrasing the question correctly is important.

    In this example (and in my suggestion), you are NOT "calling one cgi from another".

    What you are doing is presenting the user with a web page that contains links that the user can click. These links will call the second cgi. This is very different from "one cgi calling another", because there is a user interaction in between.

    Anyway, to answer this question, yes, both are possible and valid scenarios - either calling a different script directly, or after user interaction, as you are attempting.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found