Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I initialise my variables?

Status
Not open for further replies.

Kenny100

Technical User
Feb 6, 2001
72
NZ
Hi Folks

I've got a small Perl program and I use the 'require' method to read in variables from an external "config.txt" file.

The variables are $fname="kenny" and $lname="ritch".

However, when I use these variables in my Perl program (with 'use strict') I get errors that I haven't inititalised the variables. If I don't use strict, the program runs fine. Since I like using strict, how do I initialise the variables so that Perl is happy?

...
use strict

require "config.txt";

print $fname; <- How do I initialise this variable?
print $lname; <- How do I initialise this variable?
...

Cheers,
Kenny.
 
One way would be to make config.txt contain lines like:
[tt]$GLOBAL::fname = &quot;kenny&quot;;
$GLOBAL::lname = &quot;ritch&quot;;[/tt]

Your script would reference them the same way:
[tt]print &quot;$GLOBAL::fname\n&quot;;[/tt]

There's likely a better way, but this works.
 
is $GLOBAL a special varible like $_ and what does :: specify?
 
[tt]GLOBAL::[/tt] specifies the [tt]GLOBAL[/tt] namespace. You could accomplish the same thing in your config.txt by instead having it be:

[tt]package GLOBAL;
$fname = &quot;kenny&quot;;
$lname = &quot;ritch&quot;;[/tt]

[tt]$GLOBAL::fname[/tt] refers to the scalar [tt]$fname[/tt] in the GLOBAL namespace.
 
Rosenk's right, it's a namespace issue. You can do as he suggested and explicitly set the variables in the global namespace. Or you don't want to futz with explicitly declaring the namespace every time you use it, you can put it all in a package. Consider the following:
Code:
use Shared;

print $stuff;
Then in some directory in @INC(often the same directory as the script), there would be a 'Shared.pm' with the following contents:
Code:
package Shared;

BEGIN
{
	use Exporter();
	@ISA = qw(Exporter);
	@EXPORT = qw($stuff);
}

$stuff = 'asdf';

END { }
My module writing experience is limited at best, so I'll take a wary stab at explaination. The BEGIN block is code that is run as soon as file (package) is loaded. The code above sets up the exporter, and you explicitly declare in @EXPORT which variables/subroutines you want the calling program to see. @EXPORT_OK and @EXPORT_FAIL can be populated for explicit allowed or denied export, respectively.

This is the only way I've found to force the joining of the namespaces and use just bare variables that are declared in other files, but it feels kind of sloppy. If someone else has another idea, I'd love to hear it. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top