You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Jamie LeTual <ja...@hbesoftware.com> on 2001/06/06 05:47:16 UTC

Re: ´ð¸´: In module require configuration file

How about this:

#======= mymodule.pm =========
package mymodule;
use strict;

sub new
{
	my $type = shift;
	my $conf  = shift;
	
	my $self = { conf => require $conf };
	bless $self, $type;
	
	return $self;
}

1;
#======= conf.pl =========
{ web_name => 'foo.com' };

#======= conf.pl =========
#!/usr/bin/perl
use strict;
use Data::Dumper;
use mymodule;

my $m = mymodule->new('conf.pm');
print Dumper($m);

#==========================

This way you avoid having two packages (conf.pm) in the same namespace
(bad, very bad), and by specifying the conf file you want in the
constructor method (new) of mymodule.pm, you could create as many
objects as you have config files. 

Peace,
Jamie

Wang Xingyu ÍõÐËÓî [ÉϺ£] wrote:
> 
> Sorry.I find ,require different configuration file in module is
> impossibility.
> Have a mistake ,my  mandrake box run in cgi environment as a result of
> apache configure.
> 
> So ,in mod-perl,I dont find any way require different configuration.
> 
> > -----ԭʼÓʼþ-----
> > ·¢¼þÈË: Wang Xingyu ÍõÐËÓî [ÉϺ£]
> > ·¢ËÍʱ¼ä: 2001Äê6ÔÂ5ÈÕ 15:22
> > ÊÕ¼þÈË: modperl@apache.org
> > Ö÷Ìâ: In module require configuration file
> >
> >
> > Hi all:
> >
> >        In mod-perl,my module (mymodule.pm) will read config var from a
> > file ( require conf.pm).I put the require in my new() function.
> >        In one instance,they will work well.
> >        But ,I will run two or more instance (the two conf.pm is
> > different.),the mymodule.pm will have error.Because it
> > require conf.pm
> > and maybe get other conf.pm.
> >         I am test false in redhat 7,the apache is 1.3.14 and
> > mod-perl is
> > 1.24. And re-compile apache 1.3.20 and mod-perl 1.25,and test false
> > also.
> >
> >        But I am test in Mandrake 8 ,the apache is 1.3.19 and
> > mod-perl is
> > 1.25.The two instance has  worked!
> >
> >        I wonder,why redhat un-support two instance and
> > Mandrake is well?
> >
> >        Thanks and sorry for my english.
> >
> >
> > The sample code:
> >
> > =========================================================
> > #!/usr/bin/perl
> > # mymodule.pm
> >
> > Package mymodule;
> >
> > sub new
> > {
> > my $type = shift;
> > my $self = {};
> > require "./conf.pm";
> > bless $self,$type;
> > }
> >
> > =========================================================
> > #!/usr/bin/perl
> > # conf.pm
> > # configuration file one
> > package conf;
> > use strict;
> > use vars qw($conf);
> >
> > # defined configure variable
> > $conf =
> >     {
> >     # web name.
> >     'web_name' => 'foo.com',
> >      }
> >
> >
> >
> > ========================================================
> > #!/usr/bin/perl
> > # conf.pm
> > # configuration file two
> > package conf;
> > use strict;
> > use vars qw($conf);
> >
> > # defined configure variable
> > $conf =
> >     {
> >     # web name.
> >     'web_name' => 'foo.com',
> >      }
> >
> > =========================================================
> > #!/usr/bin/perl
> > # mycgi.pl
> > use mymodule;
> > my $m = mymodule->new();
> > print $conf::conf->{'web_name'};
> >
> >
> > ==========================================================
> >

-- 
-[sent from home]-----------------------------------------------
| Jamie LeTual             |Email : jamie@mail.letual.net       |
| Programmeur Errant       |Phone : (514) 523-0940              |
|                          |PGP   : http://people.hbe.ca/~jamie |
|__________________________|____________________________________|

Re: ´ð¸´: In module require configuration file [frig]

Posted by Jamie LeTual <ja...@hbesoftware.com>.
typos, sorry. Damned cut and paste

Jamie LeTual wrote:
 > 
 > How about this:
 > 
 > #======= mymodule.pm =========
 > package mymodule;
 > use strict;
 > 
 > sub new
 > {
 >         my $type = shift;
 >         my $conf  = shift;
 > 
 >         my $self = { conf => require $conf };
 >         bless $self, $type;
 > 
 >         return $self;
 > }
 > 
 > 1;
 > #======= conf.pl =========
 > { web_name => 'foo.com' };
 > 
-> #======= conf.pm =========
+> #======= somefile.pl =========
 > #!/usr/bin/perl
 > use strict;
 > use Data::Dumper;
 > use mymodule;
 > 
-> my $m = mymodule->new('conf.pm');
+> my $m = mymodule->new('conf.pl');
 > print Dumper($m);
 > 

...

-- 
-[sent from home]-----------------------------------------------
| Jamie LeTual             |Email : jamie@mail.letual.net       |
| Programmeur Errant       |Phone : (514) 523-0940              |
|                          |PGP   : http://people.hbe.ca/~jamie |
|__________________________|____________________________________|