For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > Multiple line parameters in parameter file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Multiple line parameters in parameter file
Jerry Rocteur

2006-08-02, 7:57 am

Hi,

I've just searched the web and tried the archives but I'm not in luck

I'm reading a parameter file:

PARAM=value
PARAM1=value2

Etc. and this works great, but all of a sudden...I need multiple lines..

For example:

login=logname
password=password
ftpfiles="/var/opt/file1, /var/opt/file2, /var/opt/file3
/var/opt/file4, /var/opt/file5 f/var/opt/ile6,
/var/opt/file7"
oracleftpport=2111


The file names are relative and so quite long so I prefer to put one on each line ?

I've thought of slurping in paragraph mode but I'm really stuck for a good way to do it..

Perhaps a module ?

Any ideas ?

I can do this any other way, I just need to pass these parameters, I can do it any way I want..

I prefer not having two parameter files but ...

Thanks in advance,


Jerry
Rob Dixon

2006-08-02, 7:57 am

Jerry Rocteur wrote:
>
> I've just searched the web and tried the archives but I'm not in luck
>
> I'm reading a parameter file:
>
> PARAM=value
> PARAM1=value2
>
> Etc. and this works great, but all of a sudden...I need multiple lines..
>
> For example:
>
> login=logname
> password=password
> ftpfiles="/var/opt/file1, /var/opt/file2, /var/opt/file3
> /var/opt/file4, /var/opt/file5 f/var/opt/ile6,
> /var/opt/file7"
> oracleftpport=2111
>
>
> The file names are relative and so quite long so I prefer to put one on each

line ?
>
> I've thought of slurping in paragraph mode but I'm really stuck for a good way

to do it..
>
> Perhaps a module ?
>
> Any ideas ?
>
> I can do this any other way, I just need to pass these parameters, I can do it

any way I want..
>
> I prefer not having two parameter files but ...


Hi Jerry

I started writing this in a single pass, but its a lot easier if you take two
passes - effectively converting from multi-line parameters to single-line ones.
I assume you're not expecting multi-megabyte parameter files? :)

It needs some tweaking, as your example has no comma separator after the first
line of the 'ftpfiles' parameter (do you want to check for this and add it, or
just insist on the param file being correct?); the leading whitespace is
retained, perhaps unnecessarily; and you may want to reconsider your double
quotes, as this code relies on everything not starting with 'name=' being a
continuation line and they may not be needed.

HTH,

Rob


use strict;
use warnings;

my @data;

while (<DATA> ) {

chomp;

if (/^\w+=/) {
push @data, $_;
}
else {
$data[-1] .= $_;
}
}

my %params = map { split '=', $_, 2 } @data;


__DATA__
login=logname
password=password
ftpfiles="/var/opt/file1, /var/opt/file2, /var/opt/file3
/var/opt/file4, /var/opt/file5 f/var/opt/ile6,
/var/opt/file7"
oracleftpport=2111
Roberto Etcheverry

2006-09-29, 6:57 pm

This can be made with a regular expression:

#!/usr/bin/perl
#vim: set shiftwidth=2 autoindent showmatch syntax=perl

use strict;
use warnings;

# all lines following __DATA__ go into $s
my $s;
{ local $/ = undef; $s = <DATA>; }

my %vars;
# match 1 or more alphabetics chars followed by an '=' followed by anything
# between "s or anything up to a \n
while ($s =~ /\G([[:alpha:]]+)=(?:"(.*?)"\n|(.*?)\n)/sg) {
my $k = $1;
my $v = $2? $2: $3;
$vars{$k} = [ split /\s*,\s*/s, $v ];
}

use Data::Dumper;
print Dumper(\%vars);
print "\n";
print "Value for oracleftpport is $vars{oracleftpport}[0]\n";
print "Value for ftpfiles is @{$vars{ftpfiles}}\n";

# this prints:
#$VAR1 = {
# 'password' => [
# 'password'
# ],
# 'ftpfiles' => [
# '/var/opt/file1',
# '/var/opt/file2',
# '/var/opt/file3',
# '/var/opt/file4',
# '/var/opt/file5 f/var/opt/ile6',
# '/var/opt/file7'
# ],
# 'oracleftpport' => [
# '2111'
# ],
# 'login' => [
# 'logname'
# ]
# };
#
#Value for oracleftpport is 2111
#Value for ftpfiles is /var/opt/file1 /var/opt/file2 /var/opt/file3
/var/opt/file4 /var/opt/file5 f/var/opt/ile6 /var/opt/file7

# The data
__DATA__
login=logname
password=password
ftpfiles="/var/opt/file1, /var/opt/file2, /var/opt/file3,
/var/opt/file4, /var/opt/file5 f/var/opt/ile6,
/var/opt/file7"
oracleftpport=2111

__END__



Hope it helps
Roberto


wrote:
> <pre wrap>Hi,
>
> I've just searched the web and tried the archives but I'm not in luck
>
> I'm reading a parameter file:
>
> PARAM=value
> PARAM1=value2
>
> Etc. and this works great, but all of a sudden...I need multiple lines..
>
> For example:
>
> login=logname
> password=password
> ftpfiles="/var/opt/file1, /var/opt/file2, /var/opt/file3
> /var/opt/file4, /var/opt/file5 f/var/opt/ile6,
> /var/opt/file7"
> oracleftpport=2111
>
>
> The file names are relative and so quite long so I prefer to put one
> on each line ?
>
> I've thought of slurping in paragraph mode but I'm really stuck for a
> good way to do it..
>
> Perhaps a module ?
>
> Any ideas ?
>
> I can do this any other way, I just need to pass these parameters, I
> can do it any way I want..
>
> I prefer not having two parameter files but ...
>
> Thanks in advance,
>
>
> Jerry
>
> </pre></body>
> </html>
> </html>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com