Using Variables from Another Script

J

John Smith

We have global_vars.pl which defines:

$foo = "/abc/def/hij/";


We then have temp.pl which uses $foo as follows:

#!/usr/bin/perl -w
require "./mxrt_vars.pl";
use strict;
print "$foo\n";

../temp.pl
Global symbol "$foo" requires explicit package name at ./temp.pl line
10.
Execution of ./temp.pl aborted due to compilation errors.



If I try declaring $foo in temp.pl as follows:

#!/usr/bin/perl -w
require "./mxrt_vars.pl";
use strict;

my $foo;
print "$foo\n";

./temp.pl
Use of uninitialized value at ./temp.pl line 10.

I have also tried changing $foo to my $foo in global_vars.pl; I still
got an error.
What is the correct way to use $foo, which is included in
global_vars.pl?
 
P

Paul Lalli

John said:
We have global_vars.pl which defines:

$foo = "/abc/def/hij/";


We then have temp.pl which uses $foo as follows:

#!/usr/bin/perl -w
require "./mxrt_vars.pl";
use strict;
print "$foo\n";

./temp.pl
Global symbol "$foo" requires explicit package name at ./temp.pl line
10.
Execution of ./temp.pl aborted due to compilation errors.

Either add:
our $foo;
or fully qualify it:
print "$main::foo\n";
If I try declaring $foo in temp.pl as follows:

#!/usr/bin/perl -w
require "./mxrt_vars.pl";
use strict;

my $foo;
print "$foo\n";

$foo is now a lexical variable that has absolutely *nothing* do with
the global $foo that you decalred in mxrt_vars.pl
./temp.pl
Use of uninitialized value at ./temp.pl line 10.

I have also tried changing $foo to my $foo in global_vars.pl; I still
got an error.

By definition, 'my' variables are not global.
What is the correct way to use $foo, which is included in
global_vars.pl?

In general, create a module rather than a simple 'required' file that
creates a bunch of global variables. I recommend creating a module
that creates one global hash that contains all your values, and export
that hash using Exporter

package MyConfig;
use strict;
use warnings;
use base 'Exporter';
our @EXPORT = qw/%CFG/;
our %CFG;
$CFG{foo} = 'bar';
$CFG{user} = 'mritty';
# etc;

1;

----------------------

#!/usr/bin/perl
use strict;
use warnings;
use MyConfig;

print $CFG{foo};

__END__


Paul Lalli
 
G

Guest

: We have global_vars.pl which defines:

: $foo = "/abc/def/hij/";


: We then have temp.pl which uses $foo as follows:

: #!/usr/bin/perl -w
: require "./mxrt_vars.pl";

Your script requires mxrt_vars.pl but never sees global_vars.pl. Do you
hide code from us? No wonder $foo isn't declared anywhere.

: #!/usr/bin/perl -w
: require "./mxrt_vars.pl";
: use strict;

: my $foo;
: print "$foo\n";

: ./temp.pl
: Use of uninitialized value at ./temp.pl line 10.

Again, you do not reveal the whole story. I do not see ten (10) lines here.

: I have also tried changing $foo to my $foo in global_vars.pl; I still
: got an error.
: What is the correct way to use $foo, which is included in
: global_vars.pl?

Tell your script which file name you really mean, and follow the require/
use advice in the other posting.

Regards, Oliver.
 
C

Charles DeRykus

John said:
We have global_vars.pl which defines:

$foo = "/abc/def/hij/";


We then have temp.pl which uses $foo as follows:

#!/usr/bin/perl -w

use strict; use warnings;

our $foo; # pre-declare if global_vars.pl uses package main
require "./mxrt_vars.pl";
^^^^^^^^^^^^^^^^
Did you mean "global_vars.pl" ?

( Maybe you need both require's too...? :
require "./mxrt_vars.pl";
require"/path/to/global_vars.pl"; )

If the global_vars.pl declares a separate package, you
could alternatively just package qualify $foo:

print "$some_global_pkg_name::foo\n";


hth,
 
J

John Smith

Paul said:
In general, create a module rather than a simple 'required' file that
creates a bunch of global variables. I recommend creating a module
that creates one global hash that contains all your values, and export
that hash using Exporter

package MyConfig;
use strict;
use warnings;
use base 'Exporter';
our @EXPORT = qw/%CFG/;
our %CFG;
$CFG{foo} = 'bar';
$CFG{user} = 'mritty';
# etc;

1;

----------------------

#!/usr/bin/perl
use strict;
use warnings;
use MyConfig;

print $CFG{foo};

I tried putting the global variables in a hash in global_hash.pl as
follows:
#!/usr/bin/perl -w

package MyConfig;
use strict;

use base 'Exporter';
our @EXPORT = qw/%CFG/;
our %CFG;
$CFG{foo} = 'bar';
$CFG{user} = 'mritty';

1;

then in temp.pl:
#!/usr/bin/perl -w

use strict;
use MyConfig;

print $CFG{foo};


Running temp.pl gives the following error:
.../temp.pl
Can't locate MyConfig.pm in @INC (@INC contains:
/usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503
/usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .)
at ./temp.pl line 4.
BEGIN failed--compilation aborted at ./temp.pl line 4.


How do we place the module MyConfig.pm in the @INC path?

P.S. Please assume my system admin is not upgrading to a newer version
of perl as such a upgrade will affect a lot of users and current perl
scripts.
 
A

A. Sinan Unur

....

How do we place the module MyConfig.pm in the @INC path?

If MyConfig.pm is in the same directory as your script,

perldoc FindBin

BTW, I would recommend a naming scheme based on the name of the script.
If your script is called foo.pl, I would put the config either in
FooConfig.pm or Foo/Config.pm in the script directory.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
P

Paul Lalli

John said:
Paul Lalli wrote:
I tried putting the global variables in a hash in global_hash.pl as ^^^^^^^^^^^^^^
follows:
#!/usr/bin/perl -w

package MyConfig;

The module's name is MyConfig. Therefore, the file's name must be
MyConfig.pm

Also, get rid of that shebang. That does not belong in a module.
use strict;

use base 'Exporter';
our @EXPORT = qw/%CFG/;
our %CFG;
$CFG{foo} = 'bar';
$CFG{user} = 'mritty';

1;

then in temp.pl:
#!/usr/bin/perl -w

use strict;
use MyConfig;

print $CFG{foo};


Running temp.pl gives the following error:
../temp.pl
Can't locate MyConfig.pm in @INC (@INC contains:
/usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503
/usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .)
at ./temp.pl line 4.
BEGIN failed--compilation aborted at ./temp.pl line 4.


How do we place the module MyConfig.pm in the @INC path?

Well first, you create a file named MyConfig.pm....
P.S. Please assume my system admin is not upgrading to a newer version
of perl as such a upgrade will affect a lot of users and current perl
scripts.

This has nothing to do with your issue, but is a really poor excuse for
using a piece of software that is 15 years out of date.

Install a new version of perl somewhere other than /usr/bin

Paul Lalli
 
J

John Smith

Paul said:
The module's name is MyConfig. Therefore, the file's name must be
MyConfig.pm

Also, get rid of that shebang. That does not belong in a module.

MyConfig.pm now reads:
package MyConfig;
use strict;

use base 'Exporter';
our @EXPORT = qw/%CFG/;
our %CFG;
$CFG{foo} = 'bar';
$CFG{user} = 'mritty';

1;

(I cannot use warnings without shebang since I have an old version of
perl)

perl -c MyConfig.pm gives the following errors:
Bareword "our" not allowed while "strict subs" in use at MyConfig.pm
line 5.
Array found where operator expected at MyConfig.pm line 5, at end of
line
(Do you need to predeclare our?)
syntax error at MyConfig.pm line 5, near "our @EXPORT "
Global symbol "@EXPORT" requires explicit package name at MyConfig.pm
line 5.
Bareword "our" not allowed while "strict subs" in use at MyConfig.pm
line 6.
Bareword "CFG" not allowed while "strict subs" in use at MyConfig.pm
line 6.
Operator or semicolon missing before %CFG at MyConfig.pm line 6.
Ambiguous use of % resolved as operator % at MyConfig.pm line 6.
Global symbol "%CFG" requires explicit package name at MyConfig.pm line
7.
MyConfig.pm had compilation errors.

How come MyConfig.pm thinks "our" is a bareword (rather than keyword)?
How to resolve the compilation errors?
 
J

J. Gleixner

John said:
(I cannot use warnings without shebang since I have an old version of
perl)
How come MyConfig.pm thinks "our" is a bareword (rather than keyword)?
How to resolve the compilation errors?

Because, as you stated, you have an old version of perl. One that
doesn't support 'our'. You can either upgrade or 'use vars'.

use vars qw(%CFG);
$CFG{foo} = 'bar';


For more information see: perldoc vars
 
J

John Smith

J. Gleixner said:
use vars qw(%CFG);
$CFG{foo} = 'bar';

I have changed to use vars as follows:

package MyConfig;
use strict;

use base 'Exporter';
use vars @EXPORT = qw/%CFG/;
use vars qw(%CFG);
$CFG{foo} = 'bar';
$CFG{user} = 'mritty';

1;


I got the following error:
../temp.pl
Global symbol "@EXPORT" requires explicit package name at MyConfig.pm
line 5.
BEGIN not safe after errors--compilation aborted at MyConfig.pm line 5.
BEGIN failed--compilation aborted at ./temp.pl line 3.
 
P

Paul Lalli

John said:
I have changed to use vars as follows:

package MyConfig;
use strict;

use base 'Exporter';
use vars @EXPORT = qw/%CFG/;
use vars qw(%CFG);
$CFG{foo} = 'bar';
$CFG{user} = 'mritty';

1;


I got the following error:
./temp.pl
Global symbol "@EXPORT" requires explicit package name at MyConfig.pm
line 5.
BEGIN not safe after errors--compilation aborted at MyConfig.pm line 5.
BEGIN failed--compilation aborted at ./temp.pl line 3.

I strongly suggest you start reading some documentation on your own,
rather than posting here for every single compilation error you have.

perldoc vars

Regardless....

use vars qw/@EXPORT %CFG/
@EXPORT = qw/%CFG/;

Paul Lalli
 

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
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top