...

View Full Version : Losing scope on require?



bcarl314
04-13-2009, 09:02 PM
I've got the following code...

index.cgi:



my $testvar = "hello world";
require "rewards.cgi";


then in rewards.cgi...



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

FishMonger
04-13-2009, 09:33 PM
You need to make $testvar a package global instead of a lexical var.

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.

bcarl314
04-13-2009, 10:20 PM
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:



#!/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...



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?

bazz
04-13-2009, 11:08 PM
I do it like this:

index.cgi


my $testvar = "hello world";

require "rewards.cgi";
output_rewards($testvar); #sending the value through to the second script


Then in rewards.cgi:



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


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

it is better like
sub is_user { ... }


bazz

bcarl314
04-14-2009, 01:09 AM
Good idea. I hadn't thought of that. I've been in PHP mode way too long ;)

Thanks

FishMonger
04-14-2009, 02:28 AM
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.



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum