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:
How to create a global variables

 



digioleg54
User

Jan 31, 2017, 12:31 PM

Post #1 of 14 (3195 views)
How to create a global variables Can't Post

I need to convert shell scripts to Oracle. Original shell script has several environment files, which have export some variable to have them global.
How can I convert them to Global? My idea is to put additional variables to %ENV.
For example
%ENV {
LOGDIR => "/pat/to/log/dir"
}
and add more.
Can I do that in the package or we have another way to do it?


Laurent_R
Veteran / Moderator

Jan 31, 2017, 2:03 PM

Post #2 of 14 (3193 views)
Re: [digioleg54] How to create a global variables [In reply to] Can't Post

Sorry, I don't really understand your question.

Do you mean that you need to set some environment variables?

Please explain what you're trying to do. Are you trying to query Oracle with Perl?


FishMonger
Veteran / Moderator

Jan 31, 2017, 2:06 PM

Post #3 of 14 (3193 views)
Re: [digioleg54] How to create a global variables [In reply to] Can't Post

%ENV is a built-in global variable which you can modify as needed.

Here's how to add your LOGDIR to it.

Code
$ENV{LOGDIR} = "/pat/to/log/dir";



digioleg54
User

Feb 1, 2017, 6:42 AM

Post #4 of 14 (3181 views)
Re: [Laurent_R] How to create a global variables [In reply to] Can't Post

I have to convert Shell script to Perl. In shell script we have environment files, where set environment variables: for example: export /path/to/log. In this case it is global variable for currently running program.
The same I want to make for Perl.
I see two ways to do it.
1. Add new variable to %ENV
2. Make package where set up variable, which will be seen throughout whole project.
I want to know how to do it.


BillKSmith
Veteran

Feb 1, 2017, 6:46 AM

Post #5 of 14 (3181 views)
Re: [digioleg54] How to create a global variables [In reply to] Can't Post

I am not sure what you mean by 'global'. Any process (including a shell script or a perl script) can create, modify, or delete environmental variables. FishMonger has shown you how to do it in perl. These changes are available anywhere in the process or any subprocess which it starts. It is not possible to pass those changes back to the parent process (or shell). This restriction has nothing to do with perl. It is imposed by every OS that I know of.

Note that the term 'global variable' has special meaning in perl. It has nothing at all to do with any other process. The rules for their scope and lifetime are much different from those for 'lexical variables' (defined with 'my').
Good Luck,
Bill


digioleg54
User

Feb 1, 2017, 7:30 AM

Post #6 of 14 (3179 views)
Re: [BillKSmith] How to create a global variables [In reply to] Can't Post

Can I create the Package, declare hash, using vars or our, put constant data, like servers, directories, some other data, which I will use in different Perl scripts.


digioleg54
User

Feb 1, 2017, 7:32 AM

Post #7 of 14 (3178 views)
Re: [FishMonger] How to create a global variables [In reply to] Can't Post

FishMonger, when the Perl script is finished, the data, which I put, disappeared or stays in the %ENV forever?


FishMonger
Veteran / Moderator

Feb 1, 2017, 8:09 AM

Post #8 of 14 (3170 views)
Re: [digioleg54] How to create a global variables [In reply to] Can't Post

If you make changes to the %ENV hash, those changes will go away when the script finishes.

If you need them to be permanent, you'll need to do the exporting in your profile script or if they need to be available system wide, then you put them in a startup script such as /etc/rc.local


(This post was edited by FishMonger on Feb 1, 2017, 8:09 AM)


digioleg54
User

Feb 1, 2017, 8:12 AM

Post #9 of 14 (3167 views)
Re: [FishMonger] How to create a global variables [In reply to] Can't Post

FishMonger, Can I create the Package, declare hash, using vars or our, put constant data, like servers, directories, some other data, which I will use in different Perl scripts.


FishMonger
Veteran / Moderator

Feb 1, 2017, 8:34 AM

Post #10 of 14 (3163 views)
Re: [digioleg54] How to create a global variables [In reply to] Can't Post

Yes, you can put them into a module that your perl scripts load via a use statement.

But that may not be the best approach. Based on the limited info you've given, a better approach would probably be to use a standard config file (YAML or JSON or simalar).


BillKSmith
Veteran

Feb 1, 2017, 8:37 AM

Post #11 of 14 (3162 views)
Re: [digioleg54] How to create a global variables [In reply to] Can't Post

My previous post should have answered your question to FishMonger. No, it is not possible to use environment variables to pass data to anything but a child process.

I do not believe that there is a way to fool your OS into letting you do what you seem to want. Even if you could, it would be a very bad idea. (How would you debug a program that is not in control of its own variables?)

The best way to share data among perl scripts is to use a file. I recommend that you use a format intended for persistent data (JSON or YAML). You may find it easier to use a more familiar module (Data::Dumper or Storable). If you want humans to read and/or edit the file, use the .ini format. Regardless of your choice, use a module. Do not attempt to create your own format even though that probably appears easier in the short term.
Good Luck,
Bill


digioleg54
User

Feb 1, 2017, 8:55 AM

Post #12 of 14 (3158 views)
Re: [BillKSmith] How to create a global variables [In reply to] Can't Post

My co-worker, who left the company, used Perl script, which he create variables using vars and it works. I don't know if you understand what I need. I have to convert shell scripts to Perl. In shell there are a lot of global variables in environment files, which then used in other scripts. I have to create the same in perl. I can create the same script, using ours create hash, for example:

use our (%srever_name,%logname,.....);
unless(%server_name){
$server=$nm{server},
$passwd=$ps{passw}
}
and so on.
I can use it in different scripts,
using the same line
use our (%srever_name,%logname,.....);
and I will have server name in my another script.

Am I wrong?


FishMonger
Veteran / Moderator

Feb 1, 2017, 9:03 AM

Post #13 of 14 (3156 views)
Re: [digioleg54] How to create a global variables [In reply to] Can't Post

You are wrong.

Your use statement will generate a compilation error causing your script to fail to run.


digioleg54
User

Feb 1, 2017, 9:07 AM

Post #14 of 14 (3154 views)
Re: [FishMonger] How to create a global variables [In reply to] Can't Post

But YAML has to be install with Perl. And also, can I use it amn config.yaml, or config.ini?
What it be in simple text format? Could you give me an example, please?

 
 


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

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