CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
I have a smal question about global variables

 



digioleg54
User

Feb 7, 2017, 12:13 PM

Post #1 of 9 (1861 views)
I have a smal question about global variables Can't Post

How many variables can I put to %ENV in addition to existing in it? Can be some restrictions?
If I make a package and set up %ENV in it and in perl script put use myPackage.pm will it work? Will I be able to read my variables from %ENV, which is in my Package.om?


Laurent_R
Veteran / Moderator

Feb 7, 2017, 1:28 PM

Post #2 of 9 (1857 views)
Re: [digioleg54] I have a smal question about global variables [In reply to] Can't Post

You can put as many entries as you wish in a hash, including in the %ENV global hash (at least so long as you don't exceed the memory available to your program).

If I understand your second question correctly, yes, I think that any entry you add to the %ENV hash in a module will be seen in your main program after you loaded the module, provided the code creating these entries is executed . I said "I think" because I am not sure that I've ever done that. And also because you don't say enough about how you're planning to do that. But why don't you test by yourself?

I am not sure, though, that this is the best way to do what you want, and I'm also not sure that this is a good practice, but, again, you don't say enough about what you intend to do, so it is difficult to say.


digioleg54
User

Feb 10, 2017, 11:13 AM

Post #3 of 9 (1847 views)
Re: [Laurent_R] I have a smal question about global variables [In reply to] Can't Post

I cannot say anything because I am working on the project to convert c shell to Perl. C shell has very many "export variable or path_to_dir, that I don't know what is correct approach to convert one to another. That is why I am thinking now. Do you know any site to help with conversion?


digioleg54
User

Feb 10, 2017, 12:31 PM

Post #4 of 9 (1841 views)
Re: [Laurent_R] I have a smal question about global variables [In reply to] Can't Post

What would be your advice on this: shell script is running on 10 different servers. Depends on the server I have to set up different global variables and different environment files, which have also global variables. How to make it in Perl 5.8.8


FishMonger
Veteran / Moderator

Feb 10, 2017, 12:37 PM

Post #5 of 9 (1841 views)
Re: [digioleg54] I have a smal question about global variables [In reply to] Can't Post

As I told you in your prior question, you should put these config item in a config file and use one of the many config modules on cpan to parse it.

Here's one very simple example.
env.cfg

Code
LOGDIR = /path/to/log/dir 
user = fishmonger
language = perl


test.pl

Code
#!/usr/bin/perl 

use warnings;
use strict;
use Config::General;
use Data::Dumper;

my $conf = Config::General->new("env.cfg");
my %config = $conf->getall;
print Dumper \%config;




Code
c:\test>test.pl 
$VAR1 = {
'user' => 'fishmonger',
'LOGDIR' => '/path/to/log/dir',
'language' => 'perl'
};


There are many ways to format the config file. I choose one of the simplest for tis example.


(This post was edited by FishMonger on Feb 10, 2017, 12:40 PM)


digioleg54
User

Feb 10, 2017, 12:46 PM

Post #6 of 9 (1838 views)
Re: [FishMonger] I have a smal question about global variables [In reply to] Can't Post

I don't even know if version 5.8.8 has Config. Thank you anyway


Laurent_R
Veteran / Moderator

Feb 10, 2017, 12:48 PM

Post #7 of 9 (1837 views)
Re: [digioleg54] I have a smal question about global variables [In reply to] Can't Post

Hmm, no, I don't know any such site (but maybe there are some, the only times I converted shell scripts to Perl, it was for doing things better (and faster), so the idea was to rewrite the shell script into Perl in a totally different way).

One background comment: essentially, shell scripts know only about global variables, so they are more or less bound to use global variable. That does not mean that your Perl script replacing a shell script should do the same. In fact, it is quite likely that it shouldn't (except possibly for true environment variables). But again, that's my personal conviction, this may not apply to your case. And it all depends on whether you are ready to really rewrite the application or only looking for some kind of (if possible partially automated) translation of shell into Perl.

Very recently, I made such a rewrite from shell to Perl. I replaced 900 lines of ksh and 150 lines of awk with less than 100 lines of Perl (running several times faster). But, as you can probably imagine, this was not translating shell into Perl, but it was a complete rewrite.


FishMonger
Veteran / Moderator

Feb 10, 2017, 1:03 PM

Post #8 of 9 (1835 views)
Re: [digioleg54] I have a smal question about global variables [In reply to] Can't Post


In Reply To
I don't even know if version 5.8.8 has Config. Thank you anyway


You're right, it's not a core module, but you could simply install it. If you can install your script, you can install the module.

If you don't want to install a module, then you can write your own simple parser which can be done in roughly 20 lines of code. It wouldn't be as good as one of the cpan modules, but for ou simple needs, it could be all you need.


digioleg54
User

Feb 12, 2017, 9:33 AM

Post #9 of 9 (1810 views)
Re: [Laurent_R] I have a smal question about global variables [In reply to] Can't Post

The idea is , that we got a new server, where UNIX doesn't instal. That is why around 20 shell scripts has to be convert to Perl. And Perl is old one and nobody permit me to make a revolution to install Config module. I have to check what we have and if I cannot use our old Perl scripts and modules, I have to write my own.
That is why I am thinking how to do it. She script is reading eight configuration files, working on 12 servers, that is why it has different set up for each server. I only think, how to replace those config files and how to make Perl run on 12 different servers, where perl execution directory set up in different way. Three different versions of perl.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives