Calling a scalar from another script using "require"

R

Regent

Hi, friends,

Although I read several books before posting this, I still don't know how to declare a scalar in "common.pl" and use it directly in "script.pl":

common.pl

$root = "/root";


script.pl

require "common.pl"; # this line works
print "$root"; # Global symbol "$root" requires explicit package name

PS: I habitually use the -wT switch

Regent
(e-mail address removed)
 
G

Gunnar Hjalmarsson

Regent said:
Although I read several books before posting this,

Those cannot have been Perl books... ;-)
I still don't know how to declare a scalar in "common.pl" and use
it directly in "script.pl":

common.pl

$root = "/root";


script.pl

require "common.pl"; # this line works
print "$root"; # Global symbol "$root" requires
# explicit package name

PS: I habitually use the -wT switch

Good. Since you get that error message, you are obviously using
strictures too, which is also good. :)

These are two ways to do what you want:

our $root; # declares $root as a package global
require "common.pl";
print $root;

or

require "common.pl";
print $main::root; # explicit package name

(There is no need to quote the variable in the print statement.)
 
D

David Efflandt

Hi, friends,

Although I read several books before posting this, I still don't know
how to declare a scalar in "common.pl" and use it directly in
"script.pl":

Typically require inserts the contents of the required script at the point
of the require statement. However, I noticed that if I used "my $root",
it only seemed to be in scope in the required script, and was
uninitialized in main script.
common.pl

$root = "/root";


script.pl

require "common.pl"; # this line works
print "$root"; # Global symbol "$root" requires explicit package name

PS: I habitually use the -wT switch

Regent
(e-mail address removed)

If I just used -w switch (and put a newline in print "$root\n";),
I got:

Name "main::root" used only once: possible typo at ./mytest line 3.
/root

Inserting a use vars line got rid of the warning:

use vars ('$root');
require "common.pl";
print "$root\n"

If I used -wT, "." (current dir) was apparently excluded from @INC, so I
had to use a full path to common.pl (same output).

Tested in perl v5.6.1 built for i586-linux and v5.8.0 built for
i586-linux-thread-multi
 
T

Tad McClellan

David Efflandt said:
However, I noticed that if I used "my $root",
it only seemed to be in scope in the required script, and was
uninitialized in main script.


That is what my() is _supposed to do.


perldoc -f my

... local (lexically) to the enclosing block, file ...


$root will only be in the scope of the "required file" since
my() variables can never cross file boundaries.
 
R

Regent

---------------------
Those cannot have been Perl books... ;-)

Well, those were books ABOUT Perl. The Perl books (to which I believe you refer) seem to discuss principles rather than specific problems, but I'm not much more than a newbie :((
These are two ways to do what you want:

our $root; # declares $root as a package global

You mean in common.pl? I put this line in common.pl, but it seemed I had also to declare $root in script.pl, otherwise I still got a "Global symbol "$root" requires explicit package name" error. Then I tried "my $root;" in script.pl, but got a "Use of uninitialized value", though I had already expected this error msg. The problem seems to be how do declare $root AGAIN in script.pl, which sounds weird to me :(
require "common.pl";
print $root;

or

require "common.pl";
print $main::root; # explicit package name

This worked! Thanks indeed, but what does $main here mean? "::" means the use of modules to me. BTW, $main::root appears to be a somewhat clumsy ;)
(There is no need to quote the variable in the print statement.)

Regent
(e-mail address removed)
 
G

Gunnar Hjalmarsson

Regent said:
You mean in common.pl?

No, I mean in script.pl. Both my suggestions were intended for
script.pl, while keeping common.pl as it is.
This worked! Thanks indeed, but what does $main here mean?

$main::root means the global variable $root in package main.
"::" means the use of modules to me.

When something begins with a '$' character, it's most likely a
variable. You'd better do some reading:

http://www.perldoc.com/perl5.8.0/pod/perlmod.html
 
R

Regent

------------------------
No, I mean in script.pl. Both my suggestions were intended for
script.pl, while keeping common.pl as it is.

Okay, I see. If I use this method, I must declare all scalars in script.pl like this, right?
$main::root means the global variable $root in package main.


When something begins with a '$' character, it's most likely a
variable. You'd better do some reading:

Yup, I know the '$' well, but never used "::" in this way :p
 
G

Gunnar Hjalmarsson

Regent said:
Okay, I see. If I use this method, I must declare all scalars in
script.pl like this, right?

Only those scalars you require from other files that way. Other
variables should preferrably be declared with my().
 
R

Regent

------------------------
If I just used -w switch (and put a newline in print "$root\n";),
I got:

Name "main::root" used only once: possible typo at ./mytest line 3.
/root

Inserting a use vars line got rid of the warning:

use vars ('$root');
require "common.pl";
print "$root\n"

If I used -wT, "." (current dir) was apparently excluded from @INC, so I
had to use a full path to common.pl (same output).

Yeah, this is a problem. If common.pl isn't in one of the paths already indicated in @INC, I must specify its full path in every script, right?
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top