Use in if..elsif

Discussion in 'Perl Misc' started by Andrey, Oct 21, 2003.

  1. Andrey

    Andrey Guest

    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;
    ========================================================
    Andrey, Oct 21, 2003
    #1
    1. Advertising

  2. Andrey

    ko Guest

    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
    ko, Oct 21, 2003
    #2
    1. Advertising

  3. Andrey

    Andrey Guest

    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
    Andrey, Oct 21, 2003
    #3
  4. Andrey

    Roy Johnson Guest

    "Andrey" <> wrote in message news:<bn32i8$51s$>...
    > 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.
    Roy Johnson, Oct 21, 2003
    #4
  5. Andrey

    Andrey Guest

    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.
    Andrey, Oct 26, 2003
    #5
  6. Andrey

    Sam Holden Guest

    On Mon, 27 Oct 2003 01:38:09 +0600, Andrey <> 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
    Sam Holden, Oct 26, 2003
    #6
  7. Andrey

    Andrey Guest

    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.
    Andrey, Oct 27, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. unexpected

    Avoiding if..elsif statements

    unexpected, Aug 25, 2006, in forum: Python
    Replies:
    11
    Views:
    577
    Tal Einat
    Aug 27, 2006
  2. michaelb
    Replies:
    0
    Views:
    459
    michaelb
    Sep 30, 2006
  3. karlwijk

    if/elsif problem

    karlwijk, Apr 16, 2007, in forum: VHDL
    Replies:
    3
    Views:
    659
    quantum_dot
    Apr 18, 2007
  4. palo
    Replies:
    0
    Views:
    1,290
  5. Brad Smallridge

    one hot machine without elsif

    Brad Smallridge, Feb 16, 2009, in forum: VHDL
    Replies:
    10
    Views:
    783
Loading...

Share This Page