Forum Moderators: coopster & phranque

Message Too Old, No Replies

Passing parameters to different cgi files

         

fabien007

3:17 pm on Aug 18, 2003 (gmt 0)

10+ Year Member




Hi,

I am a newbie in CGI/perl and i try to make a web interface to a MySQL database with restricted access.
I have one intro screen in which the user has to enter the MySQL user name and password, I store it in normal variables ($my_user and $my_password) and I'd like to reuse it in another cgi file, which would use these informations to do some automatic queries. Could you please give me an hint, or a link to sample scripts?
I did not really understand the way packages work, as I could not integrate it in my interface. Maybe the environnment variables would do the thing, but once again i didnt find any pratical implementation of it.

Than

claus

3:49 pm on Aug 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Say you have these in the same directory:

(a) scriptA.pl
(b) scriptB.pl

You want to use stuff from (a) inside (b). Then in (b) just write this:

#!/usr/bin/perl
use scriptA;

Hope this does the trick
/claus

fabien007

7:25 am on Aug 19, 2003 (gmt 0)

10+ Year Member



thx Claus
but the problem is that scriptA.pl would be in /usr/local/httpd/cgi-bin/ and scriptB.pm would be in /usr/lib/perl5/5.6.0/i586-linux/

Because scriptA.pl belongs and uses to the package scriptB and I couldnt manage to let both scripts in /usr/local/httpd/cgi-bin/, it complained that it could not find the package scriptB...

myself

7:37 am on Aug 19, 2003 (gmt 0)

10+ Year Member



When Perl tries to include some file or package it scans directories contained in @INC array. So if you need to include (use) scriptB placed in /some/dirB, just make the following:


push @INC, "/some/dirB";

Note. There are two alternative ways to import other files into the current script: use and require. When you write require, Perl import the text of required file when this directive appears. But as far as you put use Some; be sure to initialize @INC within BEGIN{} block: that is because directives use are processed before Perl starts to execute script. So you code should be like this:


BEGIN{
push @INC, "/some/dirB";
}

use ScriptB;
...

claus

8:33 am on Aug 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a couple of methods, i think the first should do the trick, but i couldn't help searching for alternatives:

(1)

!#/usr/bin/perl
$MyScript "/usr/local/httpd/cgi-bin/scriptA.pl"
require $MyScript;

(2)

!#/usr/bin/perl 
use lib "/usr/local/httpd/cgi-bin";
require scriptA.pl;

(3)

You can also append the scriptA directory to the search path using the shebang line it seems:

!#/usr/bin/perl -I/usr/local/httpd/cgi-bin
require scriptA.pl;

or:

!#/usr/bin/perl -I/usr/local/httpd/cgi-bin/scriptA.pl

(4)

I've also seen another syntax for this, but it's less intuitive :

!#/usr/bin/perl
BEGIN{
push(@INC,'/usr/local/httpd/cgi-bin')
}
require "scriptA.pl";

(5)

And then there's this one

do "/usr/local/httpd/cgi-bin/scriptA.pl";

/claus

Storyteller

9:56 am on Aug 19, 2003 (gmt 0)

10+ Year Member



fabien007, are these values supplied by a web site visitor via form or are they entered by you, while editing your script?

fabien007

10:31 am on Aug 19, 2003 (gmt 0)

10+ Year Member



Claus, thx again ill try your alternatives

Storyteller, the values (mysql username and password) are entered via a web form using a POST method

fabien007

11:09 am on Aug 19, 2003 (gmt 0)

10+ Year Member



OK so i try to clarify the situation:

i have a script named hello_screen.cgi that gets through a WEB form (POST METHOD) the user name $DB_user and the password $DB_pass. In hello_screen.cgi, i can access these with no problem.

i have a script named DATABROWSER.pm that plays with these vars. It is now in the same directory as hello_screen.cgi (thx to Claus and Myself ). But when i try to access to $DB_user and $DB_pass, i get blank values (ie an empty string).

Here are the piece of codes:

hello_screen.cgi

#!/usr/bin/perl -wT
#

BEGIN{
push @INC, "/usr/local/httpd/cgi-bin";
}

package DATABROWSER;
use DATABROWSER;
require "cgi-lib.pl";
require Exporter;
@ISA = qw(Exporter);

@EXPORT = qw(
$DB_user $DB_pass
);

use vars qw (
$DB_user $DB_pass
);

And i try to access directly to $DB_user and $DB_pass in DATABROWSER.pm. It compiles and runs but i get empty values for the vars. I thought i might have to do a use hello_screen but it doesnt work as hello_screen.cgi is not a pm file

myself

1:15 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



If you declared variables within hello_screen.cgi using my directive, variables will belong to main:: namespace. In file DATABROWSER.pm command package DATABROWSER tells perl to switch namespace so that variables in mail:: are no longer visible.

Try either explicitly specify namespace writing in DATABROWSER.pm:


...# within DATABROWSER.pm
print $main::DB_user;
...

or (it is better) just pass your variables as a function parameters:


...# in hello_screen.pl
login ($DB_user, $DB_pass); # login is a function defined in DATABROWSER.pm
...

...writing all that one more question appeared: when do you use $DB_name etc. in DATABROWSER.pm? In the package itself or in some of its functions?

myself

1:18 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



2. You do not need to export variables in DATABROWSER.pm, following code is redundunt (and errorneous):

@EXPORT = qw(
$DB_user $DB_pass
);

This is because you do not export these variables from package.

fabien007

1:23 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



Excuse me i should have been more explicit

In fact DATABROWSER gathers (or at least will) lots of functions such as connecttomysql

and the function connecttomysql
is called by another cgi files (option1.cgi, option2.cgi, ...)

so $DB_user and $DB_pass variables are used in a function (should i say sub function) within DATABROWSER.pm

myself

1:58 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



So that's not a problem: the simplest way is to add some function into DATABROWSER.pm that will take user name and password. Call it before any other functions that use database.

fabien007

2:13 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



but this function already exists : thats connecttomysql, which is used for every database connection

the thing i cant make work is using $DB_user and $DB_pass in this function and not blank values instead

Storyteller

4:21 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



You need to move calls to function to your .pl script. Your $DB_user and $DB_pass will show as blanks anywhere but the block in which they were declared (ie, your .pl script).

myself

6:13 am on Aug 20, 2003 (gmt 0)

10+ Year Member



This function already exists? Does its call looks like this:

...#in hello_screen.cgi
connect_db ($DB_user, $DB_pass);

?

fabien007

9:05 am on Aug 20, 2003 (gmt 0)

10+ Year Member



I dont use connect_db, which is the main db connection procedure, in hello_screen. But i use some simple routines that simply checks whether the connection is ok.

But for real world connections, as in option1.cgi, option2.cgi,... I use connect_db, which currently takes no parameter, as i dont have access to DB_user and $DB_pass in these option?.cgi (as well as in DATABROWSER.pm). Passing these variables from hello_screen.cgi to option?.cgi looks to me the same pb as passing from hello_screen.cgi to DATABROWSER.pm.