469,306 Members | 2,549 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to access a variable from other perl script

Hi,

I would like use two perl file one for execution and one for config. I want to confi the path in the config file and use it in the executing the Perl. How to access the variable from the other script.

say
In config.pm

%path=("source","c:\",
"dest","e:\");



In Copy.pl

what should i do to access and see the value.

thanks
Gilly
May 25 '07 #1
7 6862
KevinADC
4,059 Expert 2GB
Simplest way....


Copy.pl

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use vars qw(%path);
  4. require "config.pm";
May 25 '07 #2
Hi,

thanks for the Reply. Still i have some problem

As we saw before the
My package looks like this

config.pm
---------------
package config;
%configfile=("Source","E:\Source",
"Dest","E:\Dest");

1;


copy.pl
--------
#!/usr/bin/perl
use strict;
#use vars qw(%path);
require "config.pm";
print "Copy Script\n";
print $configfile{"Source"};


C:\Documents and Settings\Administrator>"E:\Copy Script.pl"
Global symbol "%configfile" requires explicit package name at E:\SENTHIL\Copy Sc
ript.pl line 6.
May 28 '07 #3
miller
1,089 Expert 1GB
Hello Gilly,

There are many things wrong with the method that you are attempting to use. I don't have time to go into explaining all of them, but I'll point out some of the major ones and give you two workable solutions to your problem.

First and most important, Config is a core module in perl. Therefore your attempted package conflicts with the core module's namespace and is causing some problems with the import. Whenever you are going to create your own package, it's always a good idea to check perldoc and cpan to see if there is already a package of that name. If there is, it's often a good idea to come up with some other name, or create your own package namespace for them all to work under.

Secondly, be careful file paths. You currently are listing your windows file paths in a double quoted string without escaping your backslashes. This will lead to a bug as those character combinations will turn into character codes \S and \D respectively. Perl provides an easy way to avoid this problem as you can use the forward slash as the path delimiter in windows boxes. Also, you should use single quoted strings for any literal strings instead of allowing interpolation. And finally, to be fully environment neatral, look into File::Spec.

Anyway, those are the two major issues. Here is a working version of your script using the Exporter module to control the importing of the hash variable properly. Note that one of your files has been renamed.

MyConfig.pm
Expand|Select|Wrap|Line Numbers
  1. package MyConfig;
  2.  
  3. use Exporter qw(import);
  4. our @EXPORT_OK = qw(%configfile);
  5.  
  6. use strict;
  7.  
  8. our %configfile = (
  9.     Source    => 'E:\Source',
  10.     Dest    => 'E:\Dest',
  11. );
  12.  
  13. 1;
  14.  
  15. __END__
  16.  
copy.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use MyConfig qw(%configfile);
  4.  
  5. use strict;
  6.  
  7. print "Copy Script\n";
  8. print $configfile{Source};
  9.  
  10. 1;
  11.  
  12. __END__
  13.  
Ok, and yes that is only one solution, but I'll add another one when I have some time.

- Miller
May 28 '07 #4
miller
1,089 Expert 1GB
Ok, that other solution.

It appears that all you're ultimately trying to do is create configuration variables. There are lots of modules available on CPAN for parsing and managing config files. I don't have any particular ones that I advocate as I ultimately created my own that is based off of XML. However, AppConfig and Config::General appear to be two ok options based off of my initial inspection and knowledge of what other modules have used. Here is an example using Config::General to do what you're attempting.

copy.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Config::General qw(ParseConfig);
  4.  
  5. use strict;
  6.  
  7. my %configfile = ParseConfig('myconfig.cfg');
  8.  
  9. print "Copy Script\n";
  10. print $configfile{Source};
  11.  
  12. 1;
  13.  
  14. __END__
  15.  
myconfig.cfg
Expand|Select|Wrap|Line Numbers
  1. Source = E:\Source
  2. Dest = E:\Dest
  3.  
- Miller
May 28 '07 #5
KevinADC
4,059 Expert 2GB
your first post has:


%path=("source","c:\",
"dest","e:\");


your second post has:

%configfile=("Source","E:\Source",
"Dest","E:\Dest");

so obviously you have to use the correct variable name: %configfile, instead of %path. But Miller posted the better suggestion and pointed out some problems. But if you wanted to use the simple solution:


#!/usr/bin/perl
use strict;
use vars qw(%configfile);
require "Myconfig.pm";
print "Copy Script\n";
print $configfile{"Source"};
May 29 '07 #6
miller
1,089 Expert 1GB
Hey Kevin,

Your simplified version would only work if MyConfig.pm did not include the package declaration. I see three different acceptable options for creating a simplified version.

1) Remove the package definition. However, If you do this, you should also rename the file to a .pl. The .pm extension is normally reserved to denote packages which is why the "use MyConfig;" knows to look for a .pm. This gives us this code.

copy.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. use vars qw(%configfile);
  6.  
  7. require 'MyConfig.pl';
  8.  
  9. print "Copy Script\n";
  10. print $configfile{Source};
  11.  
MyConfig.pl
Expand|Select|Wrap|Line Numbers
  1. our %configfile = (
  2.     Source    => 'E:\Source',
  3.     Dest    => 'E:\Dest',
  4. );
  5.  
2) Keep the package definition, but explicitly specify the namespace when refering to the hash in copy.pl.

copy.pl
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. use MyConfig;
  4.  
  5. print "Copy Script\n";
  6. print $MyConfig::configfile{Source};
  7.  
MyConfig.pm
Expand|Select|Wrap|Line Numbers
  1. package MyConfig;
  2.  
  3. our %configfile = (
  4.     Source    => 'E:\Source',
  5.     Dest    => 'E:\Dest',
  6. );
  7.  
3) Explicitly import the symbol table for the configfile hash. This would prevent the need to explicitly specify the namespace when refering to the variable.

copy.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. use MyConfig;
  6. use vars qw(%configfile); # This would also work here: our %configfile;
  7. *configfile = \%MyConfig::configfile;
  8.  
  9. print "Copy Script\n";
  10. print $configfile{Source};
  11.  
MyConfig.pm
Expand|Select|Wrap|Line Numbers
  1. package MyConfig;
  2.  
  3. our %configfile = (
  4.     Source    => 'E:\Source',
  5.     Dest    => 'E:\Dest',
  6. );
  7.  
4) Use Exporter like I demonstrated in my first post.

5) Use a config file package alternative like demonstrated in my second post.

As you can see there are lots of different ways of accomplishing this specific task. It definitely helps to have a little knowledge of packages and namespaces before venturing into working with packages. Nevertheless, these examples should enlighten you at least a little bit.

- Miller
May 29 '07 #7
KevinADC
4,059 Expert 2GB
Miller,

Yep, I agree with everything you said in your post.
May 29 '07 #8

Post your reply

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

Similar topics

4 posts views Thread by Mark Wilson CPU | last post: by
1 post views Thread by Christian Eriksson | last post: by
9 posts views Thread by Martin Foster | last post: by
2 posts views Thread by v796 | last post: by
2 posts views Thread by pleaseexplaintome | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.