- 如去年课程作业编写的一个“光线追迹”软件,需要读取的数据完全用空格和换行分隔后保存于文本文件,现在想要理解各个数据的含义就只能查看源代码了,幸好当时注释写的非常详细。
- 又如今年的一个“膜厚计算”软件。因为需要跨程序调用各个信息(cli<–>LABVIEW<–>dll),直接传递参数太复杂,因此将很多的设置项与共享数据放到了文件中。可读性太差了。
调用Config::Tiny
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
use Config:: Tiny ;
my $Config = Config:: Tiny-> new ;
$Config = Config:: Tiny-> read ( 'file.conf' ) ;
my $rootproperty = $Config -> { _ } -> { rootproperty } ;
my $one = $Config -> { section } -> { one } ;
my $Foo = $Config -> { section } -> { Foo } ;
$Config -> { newsection } = { this = > 'that' } ;
$Config -> { section } -> { Foo } = 'Not Bar!' ;
delete $Config -> { _ } ;
$Config -> write ( 'file.conf' ) ;
|
1
2
3
4
5
6
|
rootproperty = blah
[section]
one = twp
three = four
Foo = Bar
empty =
|
调用Configfile::Ini
1
2
3
4
5
6
7
8
|
use Configfile:: Ini ;
my $settingsfile = 'test.ini' ;
my $settings = new Configfile:: Ini ( $Settingsfile ) ;
my %allsettings = $settings -> get_all_settings ;
my %entry = $settings -> get_entry ( 'myentry' ) ;
my $value = $settings -> get_entry_setting ( 'myentry' , 'thissetting' ) ;
|
调用Config::IniFiles
1
2
3
4
5
6
|
use strict ;
use warnings ;
use Config:: IniFiles ;
my $cfg = new Config:: IniFiles ( - file = > "./config.ini" ) ;
print "The value is " . $cfg -> val ( 'Section' , 'Parameter' ) . "." if $cfg -> val ( 'Section' , 'Parameter' ) ;
|
调用Win32::AdminMisc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
use strict ;
use Win32:: AdminMisc ;
use Cwd ;
my $dir = getcwd ;
my $file = $dir . $ARGV [0 ] ;
foreach my $section ( Win32:: AdminMisc:: ReadINI ( $file , "" , "" ) )
{
foreach my $key ( Win32:: AdminMisc:: ReadINI ( $file , $section , "" ) )
{
my $value = Win32:: AdminMisc:: ReadINI ( $file , $section , $key ) ;
print "($key, $value)" , "n" ;
}
}
|
调用IniParser
1
2
3
|
use IniParser ;
my $ini = IniParser-> new ( "test.ini" ) ;
my $inputdir = $ini -> expectEntry ( "Directories" , "Input" ) ;
|
自己写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/usr/bin/perl -w
open CONFIG , "config.ini" || die "open config $!n" ;
$session = $ARGV [0 ] ;
$key = $ARGV [1 ] ;
$flag =0 ;
while ( $string = ) {
chomp $string ;
if ( $flag == 0 && $string ! ~ / ^ [ $session ] / ) {
next ; }
$flag = 1 ;
if ( ( $string ! ~ / ^ #/)&&(($k,$v) = $string =~ m/(w+)=(.*)/) ){
if ( $k eq $key ) {
print "I find $k ............... value $vn" ;
last ; } }
else {
next ; }
if ( $string = ~ / ^ [ / ) {
last ; }
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[perl1]
# one = 1
two = 2
three = 3
# four = four = 4
five = asd ad 5
asdasdasd
[per2]
# one = 1
two = 2
three = 3
# four = four = 4
five = I am lipeng
asdasdasd
|
1
2
|
C : UsersleniyDesktop1 > perl . pl perl1 two
I find two . . . . . . . . . . . . . . . value 2
|