> -----Original Message-----
> From: Theresa Mullin [mailto:[EMAIL PROTECTED]]
> Sent: Monday, June 24, 2002 2:30 PM
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: Running Modules
> 
> 
> Greetings All,
>  
> I have the following code, from which I attempt to call the module
> "Test"
> #!/usr/local/bin/perl
> ###
> ### Program name:  demo.pl
> ### Created By:    Theresa Mullin
>  
> require "cgi.lib";
>  
> ### Include the CGI stuff
> use CGI qw(:standard);
>  
> ### Include the database access stuff
> use DBI;
>  
> ### Push location of user created packages onto @INC array
> push(@INC,"/home/tmullin/perl/libs");  
>  
> use Test;
>  
> ### Force "no buffering" for output
> $| = 1;
>  
>  
> ### Parse the arguments sent in from the browser
> &ReadParse();
>  
> ### Open log file
> open DEMO_LOG, ">/home/tmullin/demo_log"||die "unable to open 
> log file";
>  
> print DEMO_LOG "Begin processing...\n";
> print DEMO_LOG "array INC:  @INC\n";
> print DEMO_LOG "Database selected is:  $dbase\n";
>  
>  
>  

I assume you want $dbase to have the value set below, right?

> ...Here is module Test:
>  
> #!/usr/bin/perl

Don't need this. The shell will never run Test.pm

>  
> ###  Program Name:  Test.pm
> ###  Created By:    Theresa Mullin
> ###  Date:          5/20/02
>  
> package Test;
>  

Add a "use strict" here.

> $dbase = "TEST";

Since I told you to add "use strict", you need:

   our $dbase = "TEST";

> print "And to all a good night \n";  

Modules need to end with a true value, so it's customary to have
this as the last line, which is always a true value:

   1;

>  
> The code appears to execute, and no error messages are generated.
> However, the value of $dbase is never written to the log file.
> Any advice you can give would be helpful.

The reason is that in Test.pm, you set the value of $Test::dbase,
while in your main program you access the value of $main::dbase.

So, you have two options:

1. Refer to the variable as $Test::dbase in your main program, or

2. "Export" $dbase to your main package.

Perl has a simple mechanism that allows modules like Test.pm to
"export" symbols into the calling namespace, such that when you
access $dbase, you actually are getting the value of $Test::dbase.

To do this, you would add the following lines to Test.pm, typically
right after the "package Test" line:

   require Exporter;
   our @ISA = qw(Exporter);
   our @EXPORT = qw($dbase);

Now when you "use Test" in your main program, the $dbase symbol
will be automatically exported to your namespace and everything
will Just Work.

n.b. if you are creating a module for use by others, it is now
considered "impolite" to unilaterally export symbols like this.
The preferred method is to use the @EXPORT_OK array instead of
@EXPORT:

   our @EXPORT_OK = qw($dbase);

Now, the symbol will only be exported if explicitly requested on
the use line:

   (in main program)
   
   use Test qw($dbase);

All the poop on this is in:

   perldoc Exporter

HTH

P.S. why use both cgi-lib.pl and CGI? The former is deprecated and
the latter should be used exclusively...

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to