473,385 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 10112
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

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

Similar topics

5
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
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...
4
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
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
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...
19
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
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
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 =...
9
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...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.