IniFile - Read and Write a configuration file (with encryption)
Version 2.061 - 8:31 PM 2004-10-15
use Mdc::IniFile; $F = New Mdc::IniFile( $file, $mode );
Read, update or create an MS-style configuration file with comments. File may be encrypted using a default key or with a supplied key. Output will be formatted with columns aligned for readability as the following example illustrates:
; File comment ;
hi = "Hello World!" ; string value pi = 3.14159 ; numeric value
[section1] quote = "Say \"Hello World!\"" ; This line uses embedded quotes newline = "Line one.\nLine two." ; This line has embedded newline char
[section2]
paragraph = (Use braces or parens to split huge paragraphs
onto separate lines) ; Multiline text
[section3]
hash = {key1 => "hash value 1",
key2 => "hash value 2"};
[section4] list = ["a", "b", "c", 1, 2, 3 ]
All string values written to the configuration file will be enclosed within quotes. Numeric values will not be quoted.
A comment may be assigned to any key as illustrated in the Examples section of this document. Do not use a double quote character in a comment or
If you would like to place several lines of comments at the beginning of your configuration file, that is easy. Just stuff them all, separated by new-line characters, into the _file_comment_ key and that's it. That is also illustrated in the Examples section.
It should be noted that any extraneous comments added to the file that are not in the file comment section or associated with a key will be ignored. Any subsequent updates by this module will wipe them out.
Values that contain a \n sequence will be translated into a new-line character.
The main purpose of encrypting a configuration file is to ``lock'' it so it cannot be messed with by newbie web masters. Another reason is to protect passwords, and other precious goodies from prying eyes.
A file may be encrypted using the ``EncryptOn'' method or saved in standard human readable format using ``EncryptOff'' (default). When a file is opened, it is scanned to determine if it has been encrypted. If so, a subsequent Write will save it encrypted again. The default cipher key is ``XYZ'' and may be changed by using the method CipherKey.
$F = new Mdc::IniFile( $file, $mode, $cipher_key );
Create a new object and open a configuration file. Available modes are: read, update, create. A cipher key may be supplied if the file is to be encrypted.
Close the currently opened file;
$F->Close();
Set the encryption key. Enables encryption mode when used.
$F->CipherKey( $cipherkey );
Enable file encryption.
$F->EncryptOn;
Disable file encryption.
$F->EncryptOff;
@keys = $F->Keys();
- or -
@keys = $F->Keys($section);
Return a list of the keys within the supplied section. If no section is specified, the root section keys are returned.
@sections = $F->Sections();
Return a list of all sections in the configuration file.
Create a new configuration file
use Mdc::IniFile;
$F = Open Mdc::IniFile( 'config.ini', 'create' );
Open a file to read
$F = Open Mdc::IniFile( 'config.ini' );
Open a file to update
$F = Open Mdc::IniFile( 'config.ini', 'update' );
Values are directly added by assignment.
$F->{mykey} = 'my value';
$F->{section}{mykey} = 'another value';
Comments are added using a ``_comment_'' key.
$F->{_comment_}{mykey} = 'comment for root key';
$F->{_comment_}{section}{mykey} = 'comment for section key';
Add a file comment. Will appear at the beginning of the file.
$F->{_file_comment_} = "This is a file comment";
$F->{_file_comment_} .= "\nThis is another comment";
Get the section keys
@keys = $F->Keys(); # root keys
@keys = $F->Keys(section); # section keys
Get the sections
@sec = $F->Sections();
Get a section as a hash
%hash = $F->Hash(); # root section hash
%hash = $F->Hash(section); # section hash
Encryption.
$F->EncryptOn; # Turn encryption on
$F->EncryptOff; # Turn encryption off
$F->CipherKey('abcd'); # Change the cipher key to 'abcd'
Write the file to disk.
$F->Write;
Mdc::CryptLite version 1.002+ Mdc::TextCSV version 1.04+