Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use strict, using packages and variable access

Status
Not open for further replies.

ooglek

Programmer
Jul 27, 2002
7
0
0
US
To make myself a better programmer, I am using "use strict."

Everything was fine until I split the code from the config. I want to use several configs, but 1 piece of code.

So I put the config in a file called submit6.pl which contains several variables:

my $apachelog = "/path/to/apachelog.log";
my $testing = 1;

Then there is a line like this:

use lib "/path/to/lib";
use referer_mailer;

In referer_mailer.pm, I'm trying to access the variables set in submit6.pl. If I use "our" instead of "my" I get the famed Global symbol "$apachelog" requires explicit package name at ./submit6test.cgi line 9.

How do I have to declare the variables in the program that is run (submit6.pl) so that I can access them in the code in referer_mailer.pm?

OR

How do I get/call the variables from submit6.pl from within referer_mailer.pm?

I've tried $main::apachelog, tried giving submit6.pl a package name then calling the vars as $conf::apachelog, to no avail. I used to be good with perl, but then went to PHP, and now my perl is weak.

I could fix all of this by not "use strict"ing, but I feel it is best to do it right.

Thanks
 
Ok, so I've gotten it to the point where I'm declaring all of my variables in submit6.pl:
#!/usr/bin/perl
use strict;

$conf::apachelog = '/path/to/apachelog.log';
$conf::testing = 1;

require "/home/path/referer_mailer.pm";
#use referer_mailer;
#not using use because I can't get it to work

Then in referer_mailer.pm, I refer to the vars as:

if (-w $conf::apachelog) { print "Sweet!"; }

This seems to work. But I don't think it is as nice as I can make it. I understand the variable scope, I just don't understand how to tell perl where to find the variables, nor if "my" will put the vars into the "main" package scope.

Help!
 
It's more difficult to help without knowing how referer_mailer.pm works.
I'd suggest making a subroutine in referer_mailer to do whatever it does and pass your variables to that.
Code:
#!/usr/bin/perl -w
    use strict;

    my $apachelog = '/path/to/apachelog.log';
    my $testing = 1;
    
    require "/home/path/referer_mailer.pm";
    referer_mailer_sub($apachelog, $testing);
 
referer_mailer.pm is a bunch of code I want executed. It's not in subs, so I can't just call it. What is the proper way to call the variables in the calling perl script?

Ishnid -- what you propose won't help me since referer_mailer.pm doesn't contain any subs.
 
I was suggesting creating subs in referer_mailer.pm.

Alternatively, if you declare variables using "our $foo", they can be referred by using $main::foo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top