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
-- 
Either this man is dead or my watch has stopped. - Groucho Marx

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to