Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Calling a scalar from another script using "require"

Reply
Thread Tools

Calling a scalar from another script using "require"

 
 
Regent
Guest
Posts: n/a
 
      12-29-2003
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


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-29-2003
Regent wrote:
> 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.)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
David Efflandt
Guest
Posts: n/a
 
      12-29-2003
On Mon, 29 Dec 2003 10:26:40 +0800, Regent <> wrote:
> 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
>


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

--
David Efflandt - All spam ignored http://www.de-srv.com/
PS: All mail referencing [1-9]63\.(com|net) is automatically dropped due
to excessive uncontrolled spam.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-29-2003
David Efflandt <> wrote:

> 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.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Regent
Guest
Posts: n/a
 
      12-30-2003

---------------------
Mon, 29 Dec 2003 03:58:37 +0100 £¬Gunnar Hjalmarsson wrote:
>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.)
>
>--
>Gunnar Hjalmarsson
>Email: http://www.gunnar.cc/cgi-bin/contact.pl
>


Regent


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-30-2003
Regent wrote:
> Gunnar Hjalmarsson wrote:
>>These are two ways to do what you want:
>>
>> our $root; # declares $root as a package global

>
> 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.

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

>
> 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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Regent
Guest
Posts: n/a
 
      12-30-2003

------------------------
Tue, 30 Dec 2003 01:58:53 +0100 £¬Gunnar Hjalmarsson wrote:
>Regent wrote:
>> Gunnar Hjalmarsson wrote:
>>>These are two ways to do what you want:
>>>
>>> our $root; # declares $root as a package global

>>
>> 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.


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

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

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


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

> http://www.perldoc.com/perl5.8.0/pod/perlmod.html
>
>--
>Gunnar Hjalmarsson
>Email: http://www.gunnar.cc/cgi-bin/contact.pl
>


------------------------
Regent


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-30-2003
Regent wrote:
> Gunnar Hjalmarsson wrote:
>> Regent wrote:
>>> Gunnar Hjalmarsson wrote:
>>>>
>>>> our $root; # declares $root as a package global
>>>
>>> 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.

>
> 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().

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Regent
Guest
Posts: n/a
 
      12-30-2003

------------------------
Mon, 29 Dec 2003 03:53:17 +0000 (UTC) £¬David Efflandt wrote:
>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?

>Tested in perl v5.6.1 built for i586-linux and v5.8.0 built for
>i586-linux-thread-multi
>
>--
>David Efflandt - All spam ignored http://www.de-srv.com/
>PS: All mail referencing [1-9]63\.(com|net) is automatically dropped due
>to excessive uncontrolled spam.


------------------------
Regent


 
Reply With Quote
 
Jason Hood
Guest
Posts: n/a
 
      12-30-2003
or you can include it in the @INC path

use lib('PATH','ANOTHERPATH');

J

"Regent" <> wrote in message news:bsqli7$6sh$...
>
> ------------------------
> Mon, 29 Dec 2003 03:53:17 +0000 (UTC) £¬David Efflandt wrote:
> >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?
>
> >Tested in perl v5.6.1 built for i586-linux and v5.8.0 built for
> >i586-linux-thread-multi
> >
> >--
> >David Efflandt - All spam ignored http://www.de-srv.com/
> >PS: All mail referencing [1-9]63\.(com|net) is automatically dropped due
> >to excessive uncontrolled spam.

>
> ------------------------
> Regent
>
>



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 26/12/2003
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a script from another script and other script does notdo busy wait. Rajat Python 3 01-08-2010 02:05 PM
Calling a script requiring user input from another script mzagursk@gmail.com Python 1 02-18-2009 09:18 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM
Passing value from one script on one page to another script on another page. Robert Cohen ASP General 3 07-15-2003 01:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57