Use in if..elsif

A

Andrey

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've read modperl, modperlib, Exporter but didn't find any solution.
Can anyone suggest me someting?

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;
========================================================
 
K

ko

Andrey said:
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've read modperl, modperlib, Exporter but didn't find any solution.
Can anyone suggest me someting?

perldoc autouse

Read the warnings though!

[snip code]

HTH - keith
 
A

Andrey

Hello!
Perl loads module if it sees any 'use' directive within main code before
(!) starting the programme. Try either call subs from if/else (perl will

it didn't change anything, the same problem.
not load module untill the sub will be called) or change 'use' to
'require'.

I need only 'use' because script is intended for run under mod_perl
 
R

Roy Johnson

Andrey said:
For example (from code snippets below) 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've read modperl, modperlib, Exporter but didn't find any solution.
Can anyone suggest me someting?

Documentation for "use" says:
If you don't want your namespace altered, explicitly
supply an empty list:

use Module ();

That is exactly equivalent to

BEGIN { require Module }

You might want to change the modules so that they don't export
variables, if it is up to you. Otherwise, only import the functions:
use My::Info qw(edit_info);
then refer to the variables, when you need to, with explicit package
names. Alternatively, you can refer to globals explicitly as
$::varname

I hope this answers the question you were asking.
 
A

Andrey

Hello!
if (0){
print "if\n";
}
else{
print "else\n";
my $use = "use Inc";
eval $use;
}

I tried in this way
eval ("use My::Info; &edit_info();");

It's working, 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.
 
S

Sam Holden

Hello!


I tried in this way
eval ("use My::Info; &edit_info();");

It's working, 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.

Are you using mod_perl?

If so, it loads scripts and modules on their first use and then keeps them
in the embedded perl interpreter. Since apache uses multiple processes this
means when you change things some (but not all) of the apache processes might
have the old version loaded.

Restarting the server will fix it, as will using one of the reload on change
modules (at some runtime cost).

Of course it may be something else causing your problem, but you never
know...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top