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
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...
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;
...
(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
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
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?
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
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.