How to pass global variables between files

M

Monday

Hello, I am new to Perl. I want to do something simple but seem to be
unable to do this. I want to call a subroutine and have it access the
global variables in the caller program, similar to below where
index.cgi is the calling program and called.cgi is the called program.
How do I do this since there is no 'include' type of statement in
Perl? -Monday

index.cgi
=========
#!c:/perl/bin/perl.exe
our $globalVar = "Chicago";
callNow();
return 1;

called.cgi
==========
callNow() {
print "global variable is $globalVar";
}
 
C

Chris Lowth

Monday said:
Hello, I am new to Perl. I want to do something simple but seem to be
unable to do this. I want to call a subroutine and have it access the
global variables in the caller program, similar to below where
index.cgi is the calling program and called.cgi is the called program.
How do I do this since there is no 'include' type of statement in
Perl? -Monday

index.cgi
=========
#!c:/perl/bin/perl.exe
our $globalVar = "Chicago";
callNow();
return 1;

called.cgi
==========
callNow() {
print "global variable is $globalVar";
}

you can use "do" as a simple include-ish sort of thing

in index.cgi:

do "called.cgi";

"use" is better on the whole but you need to get into the idea of packages /
modules and name spaces for that. Use "do" if you want dead simple code,
use "use" if you want to import a "package" or "module".

Chris
 
B

Brian McCauley

Hello, I am new to Perl. I want to do something simple but seem to be
unable to do this. I want to call a subroutine and have it access the
global variables in the caller program, similar to below where
index.cgi is the calling program and called.cgi is the called program.
How do I do this since there is no 'include' type of statement in
Perl?

I don't know, since you must be living in a paralell universe. In my
universe Perl has require, use and do.
index.cgi
=========
#!c:/perl/bin/perl.exe
our $globalVar = "Chicago";
callNow();
return 1;

called.cgi
==========
callNow() {
print "global variable is $globalVar";
}

It is confusing to use the file suffix ".cgi" for something that is
not a CGI script.

If you use a Perl4-style library then you can just do it.

Perl4-style libraries are simply required and don't contain a package
directive thus they are compiled in the same namespace a the file that
required them. Perl4-style libraries should not be required by
Perl5-style modules unless you really understand what you are doing.
In fact, I would go so far as to say, you sould not write Perl4-style
libraries unless you really understand why you are doing it.

Conventionally a Perl4-style library has .pl extension but it doesn't
actually matter.

Perl5-style modules can export package scoped symbols (including
variables) into the namespace of the package that use()s them. Also
calling programs can directly manipulatate a module's package scoped
symbols using a qualified name. This is the opposite of what you
asked but probably achieves the effect you are looking for better than
what you thought you wanted to do.

You can learn about Perl5 modules in "perldoc perlmod".

If you really want to access variables in the callers package
namespace then use symbolic references and the caller subroutine. It
is very rare that this is the right thing to do. Symbolic preferences
are documented in "perldoc perlref". The caller function is
documented in "perldoc -f caller".

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top