Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Use in if..elsif

Reply
Thread Tools

Use in if..elsif

 
 
Andrey
Guest
Posts: n/a
 
      10-21-2003
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:BI;

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








 
Reply With Quote
 
 
 
 
ko
Guest
Posts: n/a
 
      10-21-2003
Andrey 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.
> 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

 
Reply With Quote
 
 
 
 
Andrey
Guest
Posts: n/a
 
      10-21-2003
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



 
Reply With Quote
 
Roy Johnson
Guest
Posts: n/a
 
      10-21-2003
"Andrey" <(E-Mail Removed)> wrote in message news:<bn32i8$51s$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Andrey
Guest
Posts: n/a
 
      10-26-2003
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.


 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      10-26-2003
On Mon, 27 Oct 2003 01:38:09 +0600, Andrey <(E-Mail Removed)> wrote:
> 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.


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...

--
Sam Holden
 
Reply With Quote
 
Andrey
Guest
Posts: n/a
 
      10-27-2003
Hello!

> > 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.


> Restarting the server will fix it, as will using one of the reload on

change
> modules (at some runtime cost).


Of course I restarted server.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not use ''; file already in use. M K ASP .Net 11 04-09-2008 11:35 AM
where to use CPLD & where to use FPGA? kulkarku@math.net VHDL 6 03-06-2006 07:27 AM
How do I know when to use the Viewstate and when to use the posted data? :-) Simon ASP .Net 1 11-09-2004 02:32 AM
Can I use XPath or something to a remote Mac or Linux box and just query an xml file, not using web services and use encyrption? jake ASP .Net 0 07-06-2004 02:16 PM
Cannot use the profile "default" because it is in use, not. please.post@yur.re.ply Firefox 1 07-04-2004 03:41 AM



Advertisments