Gossamer Forum
Home : General : Perl Programming :

Global Vars

Quote Reply
Global Vars
If I create a new object in my main script (eg..$foo = new Bar), how would I allow all modules to use the variable $foo?

I have

use vars qw/$foo/;

...at the top of the main script.

I've tried local and our but I get errors with strict. Can anyone tell me how to do it?

......for example I want to be able to use $foo in Modules::Parser

@EXPORT_OK isn't what I need is it?

I've just been reading the faq's at perldoc.com but didn't find what I was looking for but I did get totally confused with all the code examples :)

I also found a certain quote funny "Did you ever notice that all young perl programmers are called Jason"

Anyway thanks.
Quote Reply
Re: [RedRum] Global Vars In reply to
Hi Paul,

You're on the right track. Do a search on CPAN (or here maybe) for the Exporter module or some varient of that. Here is [url=http://search.cpan.org/doc/GBARR/perl5.005_03/lib/Exporter.pm]one link[/url] to get you started. You can look in the documentation on modules at perl.com for info as well. I learned a fair amount on that from the 'Constructing New Perl modules..." and the "Perl Modules" docs there. Also, if you have a copy of Gossamer Mail, look through GMail.pm. Good example there. Hope that helps!

Regards,
Charlie

Last edited by:

CP: Sep 26, 2001, 3:12 PM
Quote Reply
Re: [CP] Global Vars In reply to
Thanks.

I looked at Exporter earlier. Something like:

use Exporter;

@ISA = qw(Exporter);

...etc......

Anyway I'll try what you suggested - thanks!
Quote Reply
Re: [RedRum] Global Vars In reply to
In your module you put:

use Exporter;
use vars qw/@ISA @EXPORT_OK $GLOBAL/;
@ISA = qw/Exporter/;
@EXPORT_OK = qw/$GLOBAL/;

then in your script that uses the module you can do:

use My::Module qw/$GLOBAL/;

and now it will be imported into your namespace.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Global Vars In reply to
Hi,

Thanks for the reply. What I am trying to do though is get it to work the other way around. The example above is the method of using an object created in a module, in the main script isn't it?.........I need to be able to create the object in the main script and use it in the modules.

....or would it just be better to create the object in the module and use the method above?

Let me explain what I'm trying to do to see if that helps......

In the main script, lets call it script.cgi, I have....

use vars qw/$cfg/;

$cfg = new Modules::Config;

Sub new in Modules::Config looks like:

Code:
sub new {

my ($class) = shift;
my ($self) = {};
my %defaults = (
FOO => 'bar'
);

while (($key,$val) = each %defaults) {
$self->{$key} = $val;
}

return bless $self, $class;

}

FOO => 'bar' is obviously an example but really the hash contains config settings and so I need to access these settings in some of the modules. In the main script it is fine as I can just do $cfg->{FOO} but $cfg doesn't exist in the modules and so I temporarily got around that by passing $cfg into the sub-routines I needed it in but I was looking for a better way.

Thanks for any comments.

Last edited by:

RedRum: Sep 26, 2001, 7:00 PM
Quote Reply
Re: [RedRum] Global Vars In reply to
Hi,

Quote:
foreach (keys %defaults) {
$self->{$_} = $defaults{$_};
}
You need to think oo. =) When you do:

my $cfg = new Modules::Config;

you can access $cfg options via $cfg->{option}; When you call a config method as in:

$cfg->save;

then sub save should look like:

sub save {
my $self = shift;

And $self is $cfg. So you can do print $self->{option}.

Make sense?

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Global Vars In reply to
Yeah, I know $cfg = $self in the Config module, but that won't work in other modules, will it?

If I have $cfg = new Modules::Config in script.cgi then I can't use $cfg->{FOO} in Modules::Bla nor can I use $self->{FOO} as I am using another object to access Modules::Bla like $bla = new Modules::Bla so $self would not be Modules::Config, it would be Modules::Bla - hence if I tried $self->{FOO} in Modules::Bla it would be unknown as I could only do $self->{FOO} in Config.pm

Im either not being clear, or I am getting totally confused.
Quote Reply
Re: [RedRum] Global Vars In reply to
Hi,

Ah, I think you want something like this:

Code:
package Modules::Config;

sub new {
my $class = shift;
my @opts = @_;
my $self = {};
foreach my $opt (@opts) { $self->{$_} = $opts{$_}; }
bless $self, $class;
}

package Module::Foo;

same thing

package main;
# Your main code.

my $cfg = new Modules::Config ( option1 => 'test' );
my $foo = new Modules::Foo ( cfg => $cfg );

print "Config opt1: ", $foo->{cfg}->{option1};

and inside Foo:

sub some_func {
my $self = shift;
print "Config Opt1: ", $self->{cfg}->{option1};
}

Hope that helps,

Alex
--
Gossamer Threads Inc.

Last edited by:

Alex: Sep 26, 2001, 7:14 PM
Quote Reply
Re: [Alex] Global Vars In reply to
Ahhhhhh thankyou :)

So basically I need to create the $cfg object first and then pass it into the other modules I want to use it in?

Say for example I had:

my $bla = new Modules::Bla ( cfg => $cfg );

In the main script I could then print $bla->show_me......then inside Bla.pm I could have something like:
Code:
sub show_me {

my ($self) = shift;
return $self->{cfg}->{some_key};

}
....am I on the right lines?

Thanks - we got there in the end with my cryptic explanation :)

Last edited by:

RedRum: Sep 26, 2001, 7:32 PM
Quote Reply
Re: [RedRum] Global Vars In reply to
Assuming Database.pm == Modules::Bla, exactly. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Global Vars In reply to
Whoops that was a typo :) - I just edited it but ya beat me to it!
Quote Reply
Re: [Alex] Global Vars In reply to
Should this:

my @opts = @_;

....not be

my %opts = @_;

?

Last edited by:

RedRum: Sep 26, 2001, 7:52 PM
Quote Reply
Re: [RedRum] Global Vars In reply to
Yup!

Cheers,

Alex
--
Gossamer Threads Inc.
Post deleted by RedRum In reply to

Last edited by:

RedRum: Sep 27, 2001, 6:03 AM
Quote Reply
Re: [Alex] Global Vars In reply to
Thanks Alex, that works perfectly.

I can now also use method's from other modules like $self->{CFG}->bla;