Hi there,
I have created the module and called it SimpleModule.pm.
Here is the code:
#!/usr/bin/perl package TestModule;
use DBI; use Exporter;
our @ISA = qw(Exporter); our @EXPORT = qw(connect_db); our $VERSION = 1.0;
use strict;
sub connect_db
{
my $db="testdb";
my $host="server";
my $port="111";
my $userid="test";
my $passwd="test";
my $connectionInfo="DBI:mysql:database=$db;$host:$port";
my $dbh = DBI->connect($connectionInfo,$userid,$passwd) || die("Could not get connected!");
}
1;
Here is the code in the cgi script
#!/usr/bin/perl use DBI;
use strict; use lib "."; use SimpleModule;
connect_db();
The error is: Undefined subroutine &main::connect_db called at mit.cgi line 8.
any ideas?
On Tue, 25 May 2004 13:36:34 +0200, Jan Eden <[EMAIL PROTECTED]> wrote:
Werner Otto wrote on 25.05.2004:
Im lost, could you give me an example, please...
Please cc the list...
On Tue, 25 May 2004 13:13:29 +0200, Jan Eden <[EMAIL PROTECTED]> wrote:
Hi Otto,
Werner Otto wrote on 25.05.2004:
Hi there,
Is there any way that I can call a specific sub from a cgi script.
i.e.
script1.cgi
sub print1 { print "hello1"; }
sub print2 { print "hello2"; }
script2.cgi
my messy code: require("/home/user/public_html/script1.cgi.script2()");
obviously this does not compile. But the results needed when script2.cgi runs:
perl script2.cgi output: hello2
Sounds like you need to roll your own module and put the print2 subroutine into it. Then you can utilize it for any number of scripts by
use Module;
Module::print2();
See the Exporter module if you want to avoid adding the module's name to each of the sub calls.
- Jan
Ok, your module could look like this:
=== BEGIN CODE=== package SimpleModule; use Exporter;
our @ISA = qw(Exporter); our @EXPORT = qw(print2); our $VERSION = 1.0;
use strict;
sub print2 { print "hello2"; } 1; === END CODE ===
Do not forget the "1;" on the last line, this is the return value of your module, Perl will complain if it is not there when using strict.
You can place this module in one of the paths Perl is aware of, most simply in the same directory as your script. But if you activate the taint mode via the -T switch (which is recommended for all CGI scripts), the actual directory is not searched anymore, so you should use lib:
=== BEGIN CODE === #!/usr/bin/perl -wT
use strict; use lib "."; use SimpleModule;
print2(); === END CODE ===
HTH,
Jan
-- Kind Regards, Werner Otto Web/Programming Support Department of Computer Science Kings College
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>