You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Andrey A. Kudrin" <ku...@russiancourier.com> on 2003/10/27 04:49:15 UTC
using modules in if...else
Hello ,
Hello!
I have a problem and can't get it how to solve it.
When I'm using 'use Module;' in if..elsif statement I've got situation when
all modules is included.
For example (from code snippets bellow) if I call edit_info it makes
My::Info and My::Setting to be used and I can't use global variables in
edit_info, only in last elsif statement (change_settings).
Use of global variables is critical for this code.
I tried in this way
eval ("use My::Info; &edit_info();");
It works, but in some strange manner, I need to reload page few times to
make it works, before that I see module export errors in log.
Can anyone suggest me someting?
I'll really appreciate any help.
main.cgi
=======================================================
#!/usr/bin/perl -w
package main;
use strict;
use CGI::Apache qw/:standard/;
use CGI::Cookie;
use Apache::DBI;
use lib qw (.);
use Rc::Config();
use vars qw(%cfg);
*cfg = \%Rc::Config::cfg;
use vars qw($dbh $q);
$q = new CGI;
if ($q->param('action') eq 'login')
{
&login();
}
elsif ($q->param('action') eq 'edit_info')
{
use My::Info;
&edit_info();
}
elsif ($q->param('action') eq 'change_settings')
{
use My::Settings;
&change_settings();
}
========================================================
My::Info.pm
========================================================
package My::Info;
use strict;
use lib qw(../);
use Rc::Config();
use vars qw(%cfg);
*cfg = \%Rc::Config::cfg;
use Exporter;
use vars qw(@ISA @EXPORT @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT = qw(edit_info $dbh $q);
@EXPORT_OK = qw();
use vars qw($dbh $q);
sub edit_info
{
..............................
}
1;
========================================================
My::Settings.pm
========================================================
package My::Settings;
use strict;
use lib qw(../);
use Rc::Config();
use vars qw(%cfg);
*cfg = \%Rc::Config::cfg;
use Exporter;
use vars qw(@ISA @EXPORT @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT = qw(change_settings $dbh $q);
@EXPORT_OK = qw();
use vars qw($dbh $q);
sub change_settings
{
..............................
}
1;
-------------------------------------------
Sincerely yours,
Andrey A. Kudrin,
mailto:kudrin@russiancourier.com
Re: using modules in if...else
Posted by Mark Hawkes <ha...@onetel.net.uk>.
At 11:04 2003-10-27 +0100, you wrote:
>At 09:49 2003-10-27 +0600, you wrote:
>>Hello!
>>I have a problem and can't get it how to solve it.
>>When I'm using 'use Module;' in if..elsif statement I've got situation when
>>all modules is included.
>
>'use' loads modules at compile time. To load modules conditionally, at
>runtime, try 'require' instead. EG:
>
> require DBI if $dbi_wanted;
Oops. I forgot that 'use' implicitly calls import(). To fully simulate 'use
My::Info' at runtime you should do this:
require My::Info;
My::Info->import;
This really isn't a mod_perl issue. It's just Perl stuff.
Mark
Re: using modules in if...else
Posted by Xavier Noria <fx...@hashref.com>.
El lunes, 27 octu, 2003, a las 11:04 Europe/Madrid, Mark Hawkes
escribió:
> At 09:49 2003-10-27 +0600, you wrote:
>> Hello!
>> I have a problem and can't get it how to solve it.
>> When I'm using 'use Module;' in if..elsif statement I've got
>> situation when
>> all modules is included.
>
> 'use' loads modules at compile time. To load modules conditionally, at
> runtime, try 'require' instead. EG:
>
> require DBI if $dbi_wanted;
In addition, import() has to be called explicitly if any symbol from
the module is needed. Have a look at perldoc -f use to see what's use
equivalent to.
-- fxn
Re: using modules in if...else
Posted by Mark Hawkes <ha...@onetel.net.uk>.
At 09:49 2003-10-27 +0600, you wrote:
>Hello!
>I have a problem and can't get it how to solve it.
>When I'm using 'use Module;' in if..elsif statement I've got situation when
>all modules is included.
'use' loads modules at compile time. To load modules conditionally, at
runtime, try 'require' instead. EG:
require DBI if $dbi_wanted;
hth,
Mark