CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
"require" function don't work.

 



photores
Deleted

Jan 18, 2001, 3:04 PM

Post #1 of 3 (539 views)
"require" function don't work. Can't Post

Hi!
I writing now small perl data base module, and try use for table configuration saving, make perl file with table config hash and then load it with "require" function. I write small script for testing, and it work! But if I put code in module, ...:-(. Have not any messages about errors in $!, but I can't see any variables in config file. Please, help :-)
Code samples is below:
# module code fragment:
package DB_lib;
sub insert_into {

my $self = shift;
my $t_name = shift;
my $in = shift;
my %IN = %{$in};
my $db_name = $self->{'db_name'};
my $db_dir = $self->{'db_dir'};
my(@in,$f,);
chdir "$db_dir";
require "$t_name.cfg"; # I seem, after this
print "$r_length"; # I can use all variables in this file??
}
1;
##############################################
# config file $t_name.cfg:
if($t_name =~ "image_sizes") {
$T_CFG{'ID'} = {type=>"ID",size=>"4",start_p=>0};
$T_CFG{'size'} = {type => "char",size => 30,start_p => 4};
$r_length = 36;
$last_id = 0;
}else { return 2;}




Jasmine
Administrator / Moderator

Jan 19, 2001, 10:58 AM

Post #2 of 3 (531 views)
Re: "require" function don't work. [In reply to] Can't Post

It's not working because $r_length is not available in the DB_lib's namespace.

Remember that once you create a new package, you have created a new namespace, and the namespaces don't "see" eachother unless you explicitly state the package name of the variable you want.

For example, if your configuration settings are in a package named "Vars", you can gain access to $r_length by using $Vars::r_length

By far one of the easiest ways to see what's going wrong is to use strict. That would have identified this problem immediately :)



photores
Deleted

Jan 19, 2001, 4:08 PM

Post #3 of 3 (523 views)
Re: "require" function don't work. [In reply to] Can't Post

I right understood? If I execute "require" in a module for any file, variables in the file does not become a part of namespace of a module? But, if I put in file sub get_vars{...}, and write code in module as:
require 'conf.pl';
@vars = &getvars();
Can it work?


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives