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: Beginner:
How to create properties file in Perl

 



rammohan
Novice

Mar 14, 2014, 6:40 AM

Post #1 of 8 (1424 views)
How to create properties file in Perl Can't Post

I'm newbie to this problem . How to create and read property file in Perl Script please let me know with one good example


FishMonger
Veteran / Moderator

Mar 14, 2014, 6:52 AM

Post #2 of 8 (1423 views)
Re: [rammohan] How to create properties file in Perl [In reply to] Can't Post

Your question is too vague, but I can tell you that you can use the open function to open a file in either read, write, or append mode.

See: perldoc -f open
http://perldoc.perl.org/functions/open.html


rammohan
Novice

Mar 14, 2014, 7:07 AM

Post #3 of 8 (1421 views)
Re: [FishMonger] How to create properties file in Perl [In reply to] Can't Post

I know that future has Perl. But my question is how to read properties file


BillKSmith
Veteran

Mar 14, 2014, 7:29 AM

Post #4 of 8 (1417 views)
Re: [rammohan] How to create properties file in Perl [In reply to] Can't Post

We do not understand what you mean by "properties files" Please tell us more about your application. What are you trying to accomplish?
Good Luck,
Bill


FishMonger
Veteran / Moderator

Mar 14, 2014, 7:33 AM

Post #5 of 8 (1417 views)
Re: [rammohan] How to create properties file in Perl [In reply to] Can't Post


Quote
I know that future has Perl.

That sentence doesn't make sense.


Quote
But my question is how to read parse properties file

That fully depends on how the "properties file" is formatted.

There are a number of CPAN modules for reading and writing config files in numerous formats.

Here are several choices.
http://search.cpan.org/~tlinden/Config-General-2.52/General.pm
http://search.cpan.org/~sherzodr/Config-Simple-4.59/Simple.pm
http://search.cpan.org/~rjbs/Config-INI-0.023/lib/Config/INI.pm
http://search.cpan.org/~rsavage/Config-Tiny-2.20/lib/Config/Tiny.pm


rammohan
Novice

Mar 14, 2014, 11:57 PM

Post #6 of 8 (1390 views)
Re: [BillKSmith] How to create properties file in Perl [In reply to] Can't Post

I've properties like bellow

Code
ACCOUNT_NUMBER = 100AD2 
FIRST_NAME = LARRY WALL
SECOND_NAME = PERL SCRIPT


this file in

Quote
/home/Admin/Desktop/shift.properties

. How to use this file in Perl script and what is the code for this . Please let me know


(This post was edited by rammohan on Mar 14, 2014, 11:58 PM)


Laurent_R
Veteran / Moderator

Mar 15, 2014, 3:09 AM

Post #7 of 8 (1387 views)
Re: [rammohan] How to create properties file in Perl [In reply to] Can't Post

The modules proposed by FishMonger are exactly for doing that.


(This post was edited by Laurent_R on Mar 15, 2014, 3:11 AM)


Laurent_R
Veteran / Moderator

Mar 15, 2014, 3:52 AM

Post #8 of 8 (1385 views)
Re: [rammohan] How to create properties file in Perl [In reply to] Can't Post

Altough using one of the previously mentionned modules is the best option, for such a simple case, you could do it yourself manually. For example, assuming you want to load the properties into a hash:

Code
#!/usr/bin/perl 
use warnings;
use strict;
use Data::Dumper;

my %properties;
while (<DATA>) {
chomp;
$properties{$1} = $2 if /(\w+)\s+=\s+(\w+)/;
}

print Dumper \%properties;

__DATA__
ACCOUNT_NUMBER = 100AD2
FIRST_NAME = LARRY WALL
SECOND_NAME = PERL SCRIPT

This is the dumped %properties hash:

Code
$VAR1 = { 
'SECOND_NAME' => 'PERL',
'ACCOUNT_NUMBER' => '100AD2',
'FIRST_NAME' => 'LARRY'
};

Or even simpler:

Code
use warnings; 
use strict;
use Data::Dumper;

my %properties = map { chomp; split /\s+=\s+/, $_ } <DATA>;

print Dumper \%properties;

__DATA__
ACCOUNT_NUMBER = 100AD2
FIRST_NAME = LARRY WALL
SECOND_NAME = PERL SCRIPT

which produces the same output.

 
 


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

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