user2096457
user2096457

Reputation:

How can I pass variables from one CGI script to another?

I have a CGI perl script called install-app-pl.cgi:

#!/usr/bin/perl -w

print header('text/html');

use strict;
use CGI ':standard';

# Get me some vars

my @params = param();

my $APP_NAME          = param('app_name');
my $APP_WEB_PORT      = param('app_web_port');
my $APP_WEB_USER      = param('app_web_user');
my $APP_WEB_PASS      = param('app_web_pass');
my $DOWNLOAD_DIR      = param('download_dir');
my $CONFIG_DIR        = param('config_dir');
my $LIBRARY_DIR       = param('library_dir');
my $TEMP_DOWNLOAD_DIR = param('temp_download_dir');

# Run another script

if ( $APP_NAME ) {
    print "Installing $APP_NAME...";
    print "<pre>";
    system ("perl /var/www/mysite.local/public_html/lib/$APP_NAME/install-$APP_NAME.pl");
    print "</pre>" ;
}
else {
    print "No app specified, check the error log";
}

I'm trying to get it to pass the variables defined from the CGI parameters to install-$APP_NAME.pl

#!/usr/bin/perl -w

print header('text/html');

use strict;
use CGI ':standard';

require "/var/www/mysite.local/public_html/cgi-bin/install-app-pl.cgi"

# Echo my vars

print "$CONFIG_DIR $DOWNLOAD_DIR $LIBRARY_DIR $PGID $PUID $TZ $APP_WEB_PORT";

But I'm not sure of the best way to pass those on.

Upvotes: 0

Views: 1158

Answers (1)

Dave Cross
Dave Cross

Reputation: 69244

Are you sure that install-app-pl.cgi is a CGI program? Are you sure that it's not just a Perl command-line program? I mean, I see how it's named, but it seems very strange to call a CGI program using system() like that.

And the difference is crucial here. CGI programs access their parameters in a different way command-line programs.

If it really is a CGI program, then you have a few options:

  • Make an HTTP request to it (using something from the LWP bundle of modules).
  • Use CGI.pm's debugging mechanism to call it the same way as you're currently calling it, but passing the CGI parameters like foo=xxx&bar=yyy&baz=zzz (see the DEBUGGING section of the CGI.pm documentation for details). This, of course, relies on the program using CGI.pm and it feels a bit hacky to me.
  • Ask yourself if the program really needs to be a CGI program if you're calling from another program using system(). And then decide to rewrite it as a command-line program. If you want both a CGI version and a command-line version, then you could move most of the code to a module which could be used by two thin wrappers which just extract the parameters.

A few other points about your code.

  • Perl 5.6 (released in 2000) introduced a use warnings pragma. Most people now use that in place of -w on the shebang line.
  • It seems weird to call the header() function before loading the CGI module that defines it. It works, because the use is handled at compile time, but it would be nice to re-order that code to make more sense.
  • Similarly. most people would have use strict (and use warnings) as the very first things in their program. Immediately after the shebang line.
  • system() returns the return value from the process. If your second program produces useful output that you want displayed on the web page, you should use backticks instead.
  • If all of your output is going to be in a <pre> element, why not just remove that element and return a content type of "text/plain" instead?

Update: And I'd be remiss if I didn't reiterate what many people have already said in comments on your original question - this sounds like a terrible idea.

Upvotes: 2

Related Questions