469,327 Members | 1,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

How to declare global variables.

shrek123
hi,

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 9940
eWish
971 Expert 512MB
perldoc -Constant

HTH
Oct 22 '07 #2
perldoc -Constant

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

Thanks
Oct 22 '07 #3
KevinADC
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
  1.  
  2. use strict;
  3. our $somevariable = "It is Constant";
  4.  
I have another perl file "test.pl"
Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3. my $x = $somevariable ; 
  4. print $x; # It should print It is Constant.
  5.  
  6.  
Please help me in getting the concept clear.

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

Config.pm
Expand|Select|Wrap|Line Numbers
  1. package My::Config;
  2. use base Exporter; 
  3.  
  4. our @EXPORT = qw(%config_vars);
  5. our @EXPORT_OK = qw(%config_vars);
  6.  
  7. our %config_vars = (some_var    =>    'Hello',
  8.                     another_var =>     'World'
  9.                 );
  10.  
  11.  
  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 zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.