471,873 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

How to declare global variables.


I am designing some code to communicate beetween two machines.
I want to declare variable (constants) into some seperate perl file and use them into all the perl scripts. I am confused how can I do this.
Do I need to create .pm file ? if yes please explain how can I create .pm file from .pl file.

Thanks in advance.
Oct 22 '07 #1
5 10028
971 Expert 512MB
perldoc -Constant

Oct 22 '07 #2
perldoc -Constant

I am not clear, how can I use some constant defined in some package in more then one perl script.

Also I am not clear how can I create a package with constant declaration.

Could you please expain it via some example.

Oct 22 '07 #3
4,059 Expert 2GB
Constants belong to the package they are defined in. To refer to a constant defined in another package, specify the full package name, as in Some::Package::CONSTANT.
quoted from the CONSTANT manpage
Oct 22 '07 #4
quoted from the CONSTANT manpage
Still I am not confident enough to code this. Here is what I wanna do:

I have a perl file "lib.pl" and it contain:

Expand|Select|Wrap|Line Numbers
  2. use strict;
  3. our $somevariable = "It is Constant";
I have another perl file "test.pl"
Expand|Select|Wrap|Line Numbers
  2. use strict;
  3. my $x = $somevariable ; 
  4. print $x; # It should print It is Constant.
Please help me in getting the concept clear.

Oct 23 '07 #5
971 Expert 512MB
Here is one way you can do this.

Expand|Select|Wrap|Line Numbers
  1. package My::Config;
  2. use base Exporter; 
  4. our @EXPORT = qw(%config_vars);
  5. our @EXPORT_OK = qw(%config_vars);
  7. our %config_vars = (some_var    =>    'Hello',
  8.                     another_var =>     'World'
  9.                 );
  12. 1;
In your script add these two lines to your code to access the Config.pm file.
Expand|Select|Wrap|Line Numbers
  1. use lib '/path/to/file/lib/';
  2. use My::Config;
Then you can use $config_vars{'some_var'} and $config_vars{'another_var'} in your script where you want to.

Or you can use require. Which I showed an example in another thread.
Oct 23 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Bill | last post: by
6 posts views Thread by rick | last post: by
19 posts views Thread by Hal Styli | last post: by
5 posts views Thread by John Salerno | last post: by
41 posts views Thread by Miguel Dias Moura | last post: by
5 posts views Thread by dancer | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.