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:
Scoping?

 



Andy Tomaka
Deleted

Sep 16, 2000, 8:19 AM

Post #1 of 3 (487 views)
Scoping? Can't Post

I recently decided that use strict was the next thing I needed to learn how to do. I decided to work on a script using it. However, I format my scripts with different files. I have one main script, a script that will hold the constant variables(the settings made by the user) and seperate scripts that hold the functions needed. How can I use the variables set in the settings file across the ENTIRE script? I assume I have to package them in some way, but I have no idea how to do that.

Any info would be greatly appreciated. Thank you very much in advance!

------------------
Da Wannabe Cannuck


japhy
Enthusiast

Sep 16, 2000, 8:46 AM

Post #2 of 3 (487 views)
Re: Scoping? [In reply to] Can't Post

The 'strict' pragma only effects the current scope you are in. use()ing and require()ing files executes the file in an UNRELATED scope, so while your main program may have strict on, the files you require don't need to have it on, nor will they be under the effects of strict.

However, your main program still has to behave like strict wants it to. Now, my() variables only exist in the scope in which they are defined. That means that my()ing a variable in your main program, and trying to use it in a require()d file (or vice-versa) will not work. So they'll have to be global variables. You can either preface them with a package name (main:: or the name of the package you define the variables in), or declare them with use vars.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# define.pl
package CONFIG;
$host = 'perlguru.com';
$user = 'japhy';
$password = 'not on your life';

# main.pl
use strict;
require 'define.pl';
print "Hello, $CONFIG::user.\n";
</pre><HR></BLOCKQUOTE>

Here's the other method:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# define.pl
$host = 'perlguru.com';
$user = 'japhy';
$password = 'not on your life';

# main.pl
use strict;
use vars qw( $host $user $password );
require 'define.pl';
print "Hello, $user.\n";
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



Andy Tomaka
Deleted

Sep 16, 2000, 9:12 AM

Post #3 of 3 (487 views)
Re: Scoping? [In reply to] Can't Post

Alright, thanx much! What about subroutines? Is there a way to do it without having to create a module of the libraries of subroutines??

------------------
Da Wannabe Cannuck

 
 


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

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