perl use property file for variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl use property file for variables
# 1  
Old 06-24-2010
perl use property file for variables

if I have a file that contains variables. How do I assign them in a script.

file
p=c
e=g

Thanks
# 2  
Old 06-24-2010
do you want to make those assignments in the file as perl variables? if so id' do this:

Code:
open(F,'properties') or die "failed to open file $!\n";
while (<F>){
   ($name,$val)=m/(\w+)\s*=(.+)/;
   $properties{$name}=$val;
}
close(F);

after this in your script the variable %properties wil be a hash with the name/value pairs in your file, so you can use it like:

print $properties{'a'}."\n";

is it what you want?
# 3  
Old 06-24-2010
Another approach might be to use of the many configuration file parsing modules that are available for Perl

Suppose you have a configuration fille (demo.cfg) containing
Code:
col=80
line=25

The following Perl script:
Code:
#!/usr/bin/perl

use ConfigReader::Simple;

my $config = ConfigReader::Simple->new("demo.cfg");

print "Line: ", $config->get("line"), "\n";
print "Col: ", $config->get("col"), "\n";

will output the values for both col and line:
Code:
Line: 25
Col: 80

# 4  
Old 06-24-2010
Quote:
Originally Posted by rugdog
do you want to make those assignments in the file as perl variables? if so id' do this:

Code:
open(F,'properties') or die "failed to open file $!\n";
while (<F>){
   ($name,$val)=m/(\w+)\s*=(.+)/;
   $properties{$name}=$val;
}
close(F);

after this in your script the variable %properties wil be a hash with the name/value pairs in your file, so you can use it like:

print $properties{'a'}."\n";

is it what you want?
what I needed thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Parse property from json file

Hello All, Greetings.. I have a json file that I need to pase its URLs and other values. The match should start with "notifications" and output URLs and settings values. I tried with python or awk but hardly could get URLs only. Or whole URLs from full json file. Could not match... (2 Replies)
Discussion started by: 7adi
2 Replies

2. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

3. Shell Programming and Scripting

Property file processing in unix

Hi genius, Following is the problem. We have one templete file(input file) where some variables will be used Example: word1 word2 &{word3} word4 ${word5} word6 And also we have one property file where we define these variables Example: word3= something Word5= something Need to read... (5 Replies)
Discussion started by: gjarms
5 Replies

4. Programming

Perl OLE Selection property iteration in cycle

Hi gurus, i am trying to write simple perl script using win32 ole which will iterate over all M$ word paragraphs (any text that ends with a hard return) and print only those paragraphs that matches the specified condition. The problem is that I need to access font size property. It seems to me that... (0 Replies)
Discussion started by: wakatana
0 Replies

5. Shell Programming and Scripting

Report a missing property and property value mis match script.

Hi All, I have 2 properties files - one is a master templete and other one is a node specific properties file, I need to comapre these 2 properties files and make sure the node Specific properties file contains all the properties in the master temple properties file else report the missing... (5 Replies)
Discussion started by: jayka
5 Replies

6. Shell Programming and Scripting

Perl Help - Assigning variables to text file contents

I am looking to create a perl script which will take numbers from a simple text file, convert them from decimal to hex, and then rewrite those values in the file or create a new file with the hex numbers(whichever's easier). My text document for example would be something as simple as 1312... (6 Replies)
Discussion started by: samh785
6 Replies

7. Shell Programming and Scripting

change word in a property file

Hi, I have a property file called "inspector.properties". In this property file stands the following: inspect=ON Now I want to have a shell script that when you run it, changes the ON in OFF in this property file. Is this possible with sed? Can anybody help me with this? Tnx very much. (5 Replies)
Discussion started by: thebladerunner
5 Replies

8. Shell Programming and Scripting

PERL on windows accessing variables from a config file

Folks, I'm a perl moron, so please speak very slowly. : ) I'm modifying a build script that starts up an apache server. Now there is a .config file that hardcodes an old webserver path like this c:\oldWebserver. Now I don't want that hardcoded value, rather wish to use an... (3 Replies)
Discussion started by: MarkoRocko
3 Replies

9. Shell Programming and Scripting

PERL:print 32 variables into a text file

okay, down below is the script. i have 32 words put into 32 variables and i want perl to print all those 32 variables into one text document, each word under another in the text file. the text files called times.txt Sorry about the length of the script print " VToshiba ... (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question