#! perl # Store help text in variable $syntax = "\nReadINI.pl, Version 1.10\n"; $syntax = $syntax."Read a value from the specified INI file\n\n"; $syntax = $syntax."Usage: READINI.PL \"ini_file\" \"section\" \"key\"\n\n"; $syntax = $syntax."Where: \"ini_file\" is the file name of the INI file to be read\n"; $syntax = $syntax." \"section\" is the section name, without the brackets\n"; $syntax = $syntax." \"key\" is the key whose value must be read\n\n"; $syntax = $syntax."Example: if MYPROG.INI looks like this:\n"; $syntax = $syntax."[Section 1]\n"; $syntax = $syntax."Key1=Value 1\n"; $syntax = $syntax."Key2=Value 2\n"; $syntax = $syntax."[Section 2]\n"; $syntax = $syntax."Key2=Value 3\n"; $syntax = $syntax."[Section 3]\n"; $syntax = $syntax."Key2=Value 4\n"; $syntax = $syntax."Then the command: READINI.PL \"MYPROG.INI\" \"section 2\" \"key2\"\n"; $syntax = $syntax."should return: Value 3\n\n"; $syntax = $syntax."Written by Rob van der Woude\n"; $syntax = $syntax."http://www.robvanderwoude.com\n"; # Switch to slurp mode undef $/; # Check command line arguments and open INI file if ( @ARGV[2] ) { $ini = @ARGV[0]; open( STDIN, "< $ini" ) || die "Cannot open file $ini\n$!\n\n$syntax"; $section = @ARGV[1]; $key = @ARGV[2]; } else { die "$syntax"; } # Changed ([^\n]*\n)* into ([^\[][^\n]*\n)*? after a bug report by Boby Dumoulin while ( ) { if ( $_ =~ m/^\[$section\]\s*\n([^\[][^\n]*\n)*?"?$key"?\s*=\s*"?([^\n]+)"?\s*(\n|$)/im ) { print "\n\"$ini\"\n"; print "[$section]\n"; print "$key=$2\n"; } } # Close INI file close STDIN;