Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts

    Losing scope on require?

    I've got the following code...

    index.cgi:

    Code:
    my $testvar = "hello world";
    require "rewards.cgi";
    then in rewards.cgi...

    Code:
    print "Value of test var: ";
    print $testvar;
    which prints out "Value of test var:"

    It looks like it's losing scope. Is there a way to maintain scope / references in a required file? If so, how?

    Thanks

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    You need to make $testvar a package global instead of a lexical var.
    Code:
    our $testvar = "hello world";
    However, doing so is not the best approach. If you want to post your complete code and explain your ultimate goal, we should be able to show you a better approach.

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Well, basically, the index.cgi is a monolithic 10k line file with nothing more than about 200 different functions. I'm trying to separate it out in to a more logical arrangement (i.e. all the user functions in a "user.cgi", all the rewards program functions in a "rewards.cgi" etc.

    The problem I'm having is I can't seem to keep the variables on my "required" files.

    I've tried setting things to local and our scope, but that still doesn't work.

    The biggest concern I have is with the database and session functions. In the index.cgi I start with this:

    Code:
    #!/usr/bin/perl
    use strict;
    use CGI::Carp qw(fatalsToBrowser);
    use Session;
    
    require "user.cgi";
    require "cart.cgi";
    require "admin.cgi";
    .
    .
    .
    
    my $ses = Session->new();
    
    my $f = $ses->f;
    my $db= $ses->db;
    my $op = $f->{op};
    and I'm trying to take the functions like...

    Code:
    sub is_user() {
       $result = $db->query("some query here");
       #code to validate user
    }
    but it seems like $db is lost in the required file, but not in the main (index.cgi) file.

    Does that help explain the issue?

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    I do it like this:

    index.cgi
    Code:
    my $testvar = "hello world";
    
    require "rewards.cgi";
    output_rewards($testvar); #sending the value through to the second script
    Then in rewards.cgi:

    Code:
    sub output_rewards{
    
      my $testvar = shift;  # read in the value(s) from first script
    
      print qq( Value of test var = $testvar );
    }
    if sending several vars to the second sub you can read them in like this:
    my ($varOne,$varTwo,$varThree) = @_;

    hth

    Edit:
    I notice you started your sub routine like this..
    sub is_user() { ... }

    it is better like
    sub is_user { ... }


    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #5
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Good idea. I hadn't thought of that. I've been in PHP mode way too long

    Thanks

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    I'd suggest that you turn those .cgi scripts that you load via require into true modules (either functional or OO or some combination) and load them with use instead of require.

    Before you get too far along with your current approach, you should use perldoc to read up on writing modules.

    Code:
    perlmod             Perl modules: how they work
    perlmodlib          Perl modules: how to write and use
    perlmodstyle        Perl modules: how to write modules with style


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •