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: Intermediate:
Require vs use vs do question

 



Wumbate
New User

May 7, 2008, 10:43 PM

Post #1 of 4 (752 views)
Require vs use vs do question Can't Post

Hi all,

I have a question about require, use, and do.

I have a program that is about 1000 lines of code (with comments), and am trying to break it up into multiple files. However, I have some regular expression patterns defined globally at the top that I want to keep global.

Here is a general example that should show what I'm trying to do:

file1.pl

Code
#!/usr/bin/perl -w 
use strict;

my $var="123";

require 'file2.pl';

extra(); # exists in file2.pl



file2.pl

Code
#!/usr/bin/perl -w 
use strict;

sub extra() {
print "hello $var\n"; # $var is the global variable that exists in file1.pl
return 1;
}

1;



This doesn't work as it is - test2.pl says it doesn't know what $var is.

I really want to only define the global variable once, since I don't want to later change it once but forget to change it again.

Any suggestions?


KevinR
Veteran


May 8, 2008, 9:46 AM

Post #2 of 4 (747 views)
Re: [Wumbate] Require vs use vs do question [In reply to] Can't Post

file1

Code
#!/usr/bin/perl 
use warnings;
use strict;

my $var="123";

require 'file2.pl';

extra($var); #<-- export the variable in the system array to the function


file2


Code
#!/usr/bin/perl 
use warnings;
use strict;

sub extra {
my ($var) = @_; #<-- import the variable into the function from the system array
print "hello $var\n"; # $var is the global variable that exists in file1.pl
return 1;
}

1;



Note other changes in the code. use "warnings" instead of -w, and remove the () after the function name:

sub extra() {

sub extra {

when you call a function with parenthesis after the function name you are defining a prototype instead of a function.
-------------------------------------------------


Wumbate
New User

May 8, 2008, 10:57 AM

Post #3 of 4 (742 views)
Re: [KevinR] Require vs use vs do question [In reply to] Can't Post

I'll follow your advice to "use warnings;" and omit () after function declarations. However, for the main problem I still get an error:
"Use of uninitialized value in concatenation (.) or string at file2.pl line 8."

Is there some Perl way of having Perl preinclude one file inside another before it starts processing? That way they should share globals from one if I declared in some "main" file and included through the main file.


KevinR
Veteran


May 8, 2008, 4:26 PM

Post #4 of 4 (735 views)
Re: [Wumbate] Require vs use vs do question [In reply to] Can't Post

The code I posted works fine. If you are getting a warning then you must have changed something. You can use "our" :


Code
#!/usr/bin/perl 
use warnings;
use strict;

our ($var $foo $blah %hash @array);
$var = "123";

require 'file2.pl';

extra(); # exists in file2.pl



Code
#!/usr/bin/perl 
use warnings;
use strict;

sub extra {
print "hello $var\n"; # $var is the global variable that exists in file1.pl
return 1;
}

1;

-------------------------------------------------

 
 


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

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