473,561 Members | 3,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to declare global variables.

shrek123
23 New Member
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 10118
eWish
971 Recognized Expert Contributor
perldoc -Constant

HTH
Oct 22 '07 #2
shrek123
23 New Member
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 Recognized Expert Specialist
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
shrek123
23 New Member
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 Recognized Expert Contributor
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{'s ome_var'} and $config_vars{'a nother_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

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

Similar topics

5
2906
by: NotGiven | last post by:
I have an file I call using: require_once() In this file I have variables I'd like to use in the calling page and functions called by that page. How can I do this? example:
10
6691
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for the sake of convenience (surely we learned this much from basic). here's my proposal: all "global" (document scope) variables must be declared by...
4
4755
by: Bill | last post by:
If I declared an array or object in a .JS file, should it not be global throughout all the files which reference it?
6
677
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
6
19049
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the external file, but I keep getting an error about the object does not exist. Can someone tell me where or how to declare a global variable in an...
19
3450
by: Hal Styli | last post by:
Hello, Can someone please help. I need to use a large array and I'm not sure when one should use (A) and when one should use (B) below:- #define MAX 10000000 (A) int x;
5
4708
by: John Salerno | last post by:
Here's my full code: using System; using System.Collections.Generic; using System.Text; using System.IO; namespace PatternMatcher { class Program
41
10645
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query = String.Format("SELECT * FROM dbo.documents WHERE ") & query End Sub
9
29641
by: johnlim20088 | last post by:
Hi, Hi, currently I developing an application in C# NET, let say i have a page call viewrecord.aspx, in this page, i have a global variables declared before the page_Load, so all function can access the variables. example public string record; public int status;
5
2176
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just fine. I wanted to be able to use them in another sub, so I isolated them in their own subroutine. But when I call that subroutine within another sub and then try to use those variables, it does not...
0
7639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7608
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7926
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5465
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5181
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3594
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
885
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.