home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / IniFiles.pm < prev    next >
Encoding:
Perl POD Document  |  2003-05-13  |  68.4 KB  |  2,380 lines

  1. package Config::IniFiles;
  2. $Config::IniFiles::VERSION = (qw($Revision: 2.38 $))[1];
  3. require 5.004;
  4. use strict;
  5. use Carp;
  6. use Symbol 'gensym','qualify_to_ref';   # For the 'any data type' hack
  7.  
  8. @Config::IniFiles::errors = ( );
  9.  
  10. #    $Header: /cvsroot/config-inifiles/config-inifiles/IniFiles.pm,v 2.38 2003/05/14 01:30:32 wadg Exp $
  11.  
  12. =head1 NAME
  13.  
  14. Config::IniFiles - A module for reading .ini-style configuration files.
  15.  
  16. =head1 SYNOPSIS
  17.  
  18.   use Config::IniFiles;
  19.   my $cfg = new Config::IniFiles( -file => "/path/configfile.ini" );
  20.   print "The value is " . $cfg->val( 'Section', 'Parameter' ) . "."
  21.       if $cfg->val( 'Section', 'Parameter' );
  22.  
  23. =head1 DESCRIPTION
  24.  
  25. Config::IniFiles provides a way to have readable configuration files outside
  26. your Perl script.  Configurations can be imported (inherited, stacked,...), 
  27. sections can be grouped, and settings can be accessed from a tied hash.
  28.  
  29. =head1 FILE FORMAT
  30.  
  31. INI files consist of a number of sections, each preceded with the
  32. section name in square brackets.  The first non-blank character of
  33. the line indicating a section must be a left bracket and the last
  34. non-blank character of a line indicating a section must be a right
  35. bracket. The characters making up the section name can be any 
  36. symbols at all. However section names must be unique.
  37.  
  38. Parameters are specified in each section as Name=Value.  Any spaces
  39. around the equals sign will be ignored, and the value extends to the
  40. end of the line. Parameter names are localized to the namespace of 
  41. the section, but must be unique within a section.
  42.  
  43.   [section]
  44.   Parameter=Value
  45.  
  46. Both the hash mark (#) and the semicolon (;) are comment characters.
  47. by default (this can be changed by configuration)
  48. Lines that begin with either of these characters will be ignored.  Any
  49. amount of whitespace may precede the comment character.
  50.  
  51. Multi-line or multi-valued parameters may also be defined ala UNIX 
  52. "here document" syntax:
  53.  
  54.   Parameter=<<EOT
  55.   value/line 1
  56.   value/line 2
  57.   EOT
  58.  
  59. You may use any string you want in place of "EOT".  Note that what
  60. follows the "<<" and what appears at the end of the text MUST match
  61. exactly, including any trailing whitespace.
  62.  
  63. As a configuration option (default is off), continuation lines can
  64. be allowed:
  65.  
  66.   [Section]
  67.   Parameter=this parameter \
  68.     spreads across \
  69.     a few lines
  70.  
  71.  
  72. =head1 USAGE -- Object Interface
  73.  
  74. Get a new Config::IniFiles object with the I<new> method:
  75.  
  76.   $cfg = Config::IniFiles->new( -file => "/path/configfile.ini" );
  77.   $cfg = new Config::IniFiles -file => "/path/configfile.ini";
  78.  
  79. Optional named parameters may be specified after the configuration
  80. file name.  See the I<new> in the B<METHODS> section, below.
  81.  
  82. Values from the config file are fetched with the val method:
  83.  
  84.   $value = $cfg->val('Section', 'Parameter');
  85.  
  86. If you want a multi-line/value field returned as an array, just
  87. specify an array as the receiver:
  88.  
  89.   @values = $cfg->val('Section', 'Parameter');
  90.  
  91. =head1 METHODS
  92.  
  93. =head2 new ( [-option=>value ...] )
  94.  
  95. Returns a new configuration object (or "undef" if the configuration
  96. file has an error).  One Config::IniFiles object is required per configuration
  97. file.  The following named parameters are available:
  98.  
  99. =over 10
  100.  
  101. =item I<-file>  filename
  102.  
  103. Specifies a file to load the parameters from. This 'file' may actually be 
  104. any of the following things:
  105.  
  106.   1) a simple filehandle, such as STDIN
  107.   2) a filehandle glob, such as *CONFIG
  108.   3) a reference to a glob, such as \*CONFIG
  109.   4) an IO::File object
  110.   5) the pathname of a file
  111.  
  112. If this option is not specified, (i.e. you are creating a config file from scratch) 
  113. you must specify a target file using SetFileName in order to save the parameters.
  114.  
  115. =item I<-default> section
  116.  
  117. Specifies a section to be used for default values. For example, if you
  118. look up the "permissions" parameter in the "users" section, but there
  119. is none, Config::IniFiles will look to your default section for a "permissions"
  120. value before returning undef.
  121.  
  122. =item I<-reloadwarn> 0|1
  123.  
  124. Set -reloadwarn => 1 to enable a warning message (output to STDERR)
  125. whenever the config file is reloaded.  The reload message is of the
  126. form:
  127.  
  128.   PID <PID> reloading config file <file> at YYYY.MM.DD HH:MM:SS
  129.  
  130. Default behavior is to not warn (i.e. -reloadwarn => 0).
  131.  
  132. =item I<-nocase> 0|1
  133.  
  134. Set -nocase => 1 to handle the config file in a case-insensitive
  135. manner (case in values is preserved, however).  By default, config
  136. files are case-sensitive (i.e., a section named 'Test' is not the same
  137. as a section named 'test').  Note that there is an added overhead for
  138. turning off case sensitivity.
  139.  
  140. =item I<-allowcontinue> 0|1
  141.  
  142. Set -allowcontinue => 1 to enable continuation lines in the config file.
  143. i.e. if a line ends with a backslash C<\>, then the following line is
  144. appended to the parameter value, dropping the backslash and the newline
  145. character(s).
  146.  
  147. Default behavior is to keep a trailing backslash C<\> as a parameter
  148. value. Note that continuation cannot be mixed with the "here" value
  149. syntax.
  150.  
  151. =item I<-import> object
  152.  
  153. This allows you to import or inherit existing setting from another 
  154. Config::IniFiles object. When importing settings from another object, 
  155. sections with the same name will be merged and parameters that are 
  156. defined in both the imported object and the I<-file> will take the 
  157. value of given in the I<-file>. 
  158.  
  159. If a I<-default> section is also given on this call, and it does not 
  160. coincide with the default of the imported object, the new default 
  161. section will be used instead. If no I<-default> section is given, 
  162. then the default of the imported object will be used.
  163.  
  164. =item I<-commentchar> 'char'
  165.  
  166. The default comment character is C<#>. You may change this by specifying
  167. this option to an arbitrary character, except alphanumeric characters
  168. and square brackets and the "equal" sign.
  169.  
  170. =item I<-allowedcommentchars> 'chars'
  171.  
  172. Allowed default comment characters are C<#> and C<;>. By specifying this
  173. option you may enlarge or narrow this range to a set of characters
  174. (concatenating them to a string). Note that the character specified by
  175. B<-commentchar> (see above) is always part of the allowed comment
  176. characters. Note: The given string is evaluated as a character class
  177. (i.e.: like C</[chars]/>).
  178.  
  179. =back
  180.  
  181. =cut
  182.  
  183. sub new {
  184.   my $class = shift;
  185.   my %parms = @_;
  186.  
  187.   my $errs = 0;
  188.   my @groups = ( );
  189.  
  190.   my $self           = {};
  191.   # Set config file to default value, which is nothing
  192.   $self->{cf}        = undef;
  193.   if( ref($parms{-import}) && ($parms{-import}->isa('Config::IniFiles')) ) {
  194.     # Import from the import object by COPYing, so we
  195.     # don't clobber the old object
  196.     %{$self} = %{$parms{-import}};
  197.   } else {
  198.     $self->{firstload} = 1;
  199.     $self->{default}   = '';
  200.     $self->{imported}  = [];
  201.     if( defined $parms{-import} ) {
  202.       carp "Invalid -import value \"$parms{-import}\" was ignored.";
  203.       delete $parms{-import};
  204.     } # end if
  205.   } # end if
  206.  
  207.   # Copy the original parameters so we 
  208.   # can use them when we build new sections 
  209.   %{$self->{startup_settings}} = %parms;
  210.  
  211.   # Parse options
  212.   my($k, $v);
  213.   local $_;
  214.   $self->{nocase} = 0;
  215.  
  216.   # Handle known parameters first in this order, 
  217.   # because each() could return parameters in any order
  218.   if (defined ($v = delete $parms{'-import'})) {
  219.     # Store the imported object's file parameter for reload
  220.     if( $self->{cf} ) {
  221.         push( @{$self->{imported}}, $self->{cf} );
  222.     } else {
  223.         push( @{$self->{imported}}, "<Un-named file>" );
  224.     } # end if
  225.   }
  226.   if (defined ($v = delete $parms{'-file'})) {
  227.     # Should we be pedantic and check that the file exists?
  228.     # .. no, because now it could be a handle, IO:: object or something else
  229.     $self->{cf} = $v;
  230.   }
  231.   if (defined ($v = delete $parms{'-default'})) {
  232.     $self->{default} = $v;
  233.   }
  234.   if (defined ($v = delete $parms{'-nocase'})) {
  235.     $self->{nocase} = $v ? 1 : 0;
  236.   }
  237.   if (defined ($v = delete $parms{'-reloadwarn'})) {
  238.     $self->{reloadwarn} = $v ? 1 : 0;
  239.   }
  240.   if (defined ($v = delete $parms{'-allowcontinue'})) {
  241.     $self->{allowcontinue} = $v ? 1 : 0;
  242.   }
  243.   if (defined ($v = delete $parms{'-commentchar'})) {
  244.     if(!defined $v || length($v) != 1) {
  245.       carp "Comment character must be unique.";
  246.       $errs++;
  247.     }
  248.     elsif($v =~ /[\[\]=\w]/) {
  249.       # must not be square bracket, equal sign or alphanumeric
  250.       carp "Illegal comment character.";
  251.       $errs++;
  252.     } 
  253.     else {
  254.       $self->{comment_char} = $v;
  255.     }
  256.   }
  257.   if (defined ($v = delete $parms{'-allowedcommentchars'})) {
  258.     # must not be square bracket, equal sign or alphanumeric
  259.     if(!defined $v || $v =~ /[\[\]=\w]/) {
  260.       carp "Illegal value for -allowedcommentchars.";
  261.       $errs++;
  262.     }
  263.     else {
  264.       $self->{comment_char} = $v;
  265.     }
  266.   }
  267.   $self->{comment_char} = '#' unless exists $self->{comment_char};
  268.   $self->{allowed_comment_char} = ';' unless exists $self->{allowed_comment_char};
  269.   # make sure that comment character is always allowed
  270.   $self->{allowed_comment_char} .= $self->{comment_char};
  271.  
  272.   # Any other parameters are unkown
  273.   while (($k, $v) = each %parms) {
  274.     carp "Unknown named parameter $k=>$v";
  275.     $errs++;
  276.   }
  277.  
  278.   return undef if $errs;
  279.  
  280.   bless $self, $class;
  281.  
  282.   # No config file specified, so everything's okay so far.
  283.   if (not defined $self->{cf}) {
  284.     return $self;
  285.   }
  286.   
  287.   if ($self->ReadConfig) {
  288.     return $self;
  289.   } else {
  290.     return undef;
  291.   }
  292. }
  293.  
  294. =head2 val ($section, $parameter [, $default] )
  295.  
  296. Returns the value of the specified parameter (C<$parameter>) in section 
  297. C<$section>, returns undef (or C<$default> if specified) if no section or 
  298. no parameter for the given section section exists.
  299.  
  300.  
  301. If you want a multi-line/value field returned as an array, just
  302. specify an array as the receiver:
  303.  
  304.   @values = $cfg->val('Section', 'Parameter');
  305.  
  306. A multi-line/value field that is returned in a scalar context will be
  307. joined using $/ (input record separator, default is \n) if defined,
  308. otherwise the values will be joined using \n.
  309.  
  310. =cut
  311.  
  312. sub val {
  313.   my ($self, $sect, $parm, $def) = @_;
  314.  
  315.   # Always return undef on bad parameters
  316.   return undef if not defined $sect;
  317.   return undef if not defined $parm;
  318.   
  319.   if ($self->{nocase}) {
  320.     $sect = lc($sect);
  321.     $parm = lc($parm);
  322.   }
  323.   
  324.   my $val = defined($self->{v}{$sect}{$parm}) ?
  325.     $self->{v}{$sect}{$parm} :
  326.     $self->{v}{$self->{default}}{$parm};
  327.   
  328.   # If the value is undef, make it $def instead (which could just be undef)
  329.   $val = $def unless defined $val;
  330.   
  331.   # Return the value in the desired context
  332.   if (wantarray and ref($val) eq "ARRAY") {
  333.     return @$val;
  334.   } elsif (ref($val) eq "ARRAY") {
  335.       if (defined ($/)) {
  336.         return join "$/", @$val;
  337.     } else {
  338.         return join "\n", @$val;
  339.     }
  340.   } else {
  341.     return $val;
  342.   }
  343. }
  344.  
  345. =head2 setval ($section, $parameter, $value, [ $value2, ... ])
  346.  
  347. Sets the value of parameter C<$parameter> in section C<$section> to 
  348. C<$value> (or to a set of values).  See below for methods to write 
  349. the new configuration back out to a file.
  350.  
  351. You may not set a parameter that didn't exist in the original
  352. configuration file.  B<setval> will return I<undef> if this is
  353. attempted. See B<newval> below to do this. Otherwise, it returns 1.
  354.  
  355. =cut
  356.  
  357. sub setval {
  358.   my $self = shift;
  359.   my $sect = shift;
  360.   my $parm = shift;
  361.   my @val  = @_;
  362.  
  363.   return undef if not defined $sect;
  364.   return undef if not defined $parm;
  365.  
  366. # tom@ytram.com +
  367.   if ($self->{nocase}) {
  368.     $sect = lc($sect);
  369.     $parm = lc($parm);
  370.   }
  371. # tom@ytram.com -
  372.  
  373.   if (defined($self->{v}{$sect}{$parm})) {
  374.     if (@val > 1) {
  375.       $self->{v}{$sect}{$parm} = \@val;
  376.       $self->{EOT}{$sect}{$parm} = 'EOT';
  377.     } else {
  378.       $self->{v}{$sect}{$parm} = shift @val;
  379.     }
  380.     return 1;
  381.   } else {
  382.     return undef;
  383.   }
  384. }
  385.  
  386. =head2 newval($section, $parameter, $value [, $value2, ...])
  387.  
  388. Assignes a new value, C<$value> (or set of values) to the 
  389. parameter C<$parameter> in section C<$section> in the configuration 
  390. file.
  391.  
  392. =cut
  393.  
  394. sub newval {
  395.   my $self = shift;
  396.   my $sect = shift;
  397.   my $parm = shift;
  398.   my @val  = @_;
  399.   
  400.   return undef if not defined $sect;
  401.   return undef if not defined $parm;
  402.  
  403. # tom@ytram.com +
  404.   if ($self->{nocase}) {
  405.     $sect = lc($sect);
  406.     $parm = lc($parm);
  407.   }
  408. # tom@ytram.com -
  409.     $self->AddSection($sect);
  410.  
  411.     push(@{$self->{parms}{$sect}}, $parm) 
  412.       unless (grep {/^\Q$parm\E$/} @{$self->{parms}{$sect}} );
  413.  
  414.   if (@val > 1) {
  415.     $self->{v}{$sect}{$parm} = \@val;
  416.     $self->{EOT}{$sect}{$parm} = 'EOT' unless defined
  417.                 $self->{EOT}{$sect}{$parm};
  418.   } else {
  419.     $self->{v}{$sect}{$parm} = shift @val;
  420.   }
  421.   return 1
  422. }
  423.  
  424. =head2 delval($section, $parameter)
  425.  
  426. Deletes the specified parameter from the configuration file
  427.  
  428. =cut
  429.  
  430. sub delval {
  431.   my $self = shift;
  432.   my $sect = shift;
  433.   my $parm = shift;
  434.   
  435.   return undef if not defined $sect;
  436.   return undef if not defined $parm;
  437.  
  438. # tom@ytram.com +
  439.   if ($self->{nocase}) {
  440.     $sect = lc($sect);
  441.     $parm = lc($parm);
  442.   }
  443. # tom@ytram.com -
  444.  
  445.     @{$self->{parms}{$sect}} = grep !/^\Q$parm\E$/, @{$self->{parms}{$sect}};
  446.     delete $self->{v}{$sect}{$parm};
  447.     return 1
  448. }
  449.  
  450. =head2 ReadConfig
  451.  
  452. Forces the configuration file to be re-read. Returns undef if the 
  453. file can not be opened, no filename was defined (with the C<-file>
  454. option) when the object was constructed, or an error occurred while 
  455. reading.
  456.  
  457. If an error occurs while parsing the INI file the @Config::IniFiles::errors
  458. array will contain messages that might help you figure out where the 
  459. problem is in the file.
  460.  
  461. =cut
  462.  
  463. sub ReadConfig {
  464.   my $self = shift;
  465.  
  466.   my($lineno, $sect);
  467.   my($group, $groupmem);
  468.   my($parm, $val);
  469.   my @cmts;
  470.   my %loaded_params = ();            # A has to remember which params are loaded vs. imported
  471.   @Config::IniFiles::errors = ( );
  472.  
  473.   # Initialize (and clear out) storage hashes
  474.   # unless we imported them from another file [JW]
  475.   if( @{$self->{imported}} ) {
  476.       #
  477.       # Run up the import tree to the top, then reload coming
  478.       # back down, maintaining the imported file names and our 
  479.       # file name.
  480.       # This is only needed on a re-load though
  481.       unless( $self->{firstload} ) {
  482.         my $cf = $self->{cf};
  483.         $self->{cf} = pop @{$self->{imported}};
  484.         $self->ReadConfig;
  485.         push @{$self->{imported}}, $self->{cf};
  486.         $self->{cf} = $cf;
  487.       } # end unless
  488.   } else {
  489.       $self->{sects}  = [];        # Sections
  490.       $self->{group}  = {};        # Subsection lists
  491.       $self->{v}      = {};        # Parameter values
  492.       $self->{sCMT}   = {};        # Comments above section
  493.   } # end if
  494.   
  495.   return undef if (
  496.     (not exists $self->{cf}) or
  497.     (not defined $self->{cf}) or
  498.     ($self->{cf} eq '')
  499.   );
  500.   
  501.   my $nocase = $self->{nocase};
  502.  
  503.   # If this is a reload and we want warnings then send one to the STDERR log
  504.   unless( $self->{firstload} || !$self->{reloadwarn} ) {
  505.     my ($ss, $mm, $hh, $DD, $MM, $YY) = (localtime(time))[0..5];
  506.     printf STDERR
  507.       "PID %d reloading config file %s at %d.%02d.%02d %02d:%02d:%02d\n",
  508.       $$, $self->{cf}, $YY+1900, $MM+1, $DD, $hh, $mm, $ss;
  509.   }
  510.   
  511.   # Turn off. Future loads are reloads
  512.   $self->{firstload} = 0;
  513.  
  514.   # Get a filehandle, allowing almost any type of 'file' parameter
  515.   my $fh = $self->_make_filehandle( $self->{cf} );
  516.   if (!$fh) {
  517.     carp "Failed to open $self->{cf}: $!";
  518.     return undef;
  519.   }
  520.   
  521.   # Get mod time of file so we can retain it (if not from STDIN)
  522.   my @stats = stat $fh;
  523.   $self->{file_mode} = sprintf("%04o", $stats[2]) if defined $stats[2];
  524.   
  525.   # Get the entire file into memory (let's hope it's small!)
  526.   local $_;
  527.   my @lines = split /\015\012?|\012|\025|\n/, join( '', <$fh>);
  528.   
  529.   # Only close if this is a filename, if it's
  530.   # an open handle, then just roll back to the start
  531.   if( !ref($fh) ) {
  532.     close($fh);
  533.   } else {
  534.     # But we can't roll back STDIN so skip that one
  535.     if( $fh != 0 ) {
  536.       seek( $fh, 0, 0 );
  537.     } # end if
  538.   } # end if
  539.  
  540.   # If there's a UTF BOM (Byte-Order-Mark) in the first character of the first line
  541.   # then remove it before processing (http://www.unicode.org/unicode/faq/utf_bom.html#22)
  542.   ($lines[0] =~ s/^∩╗┐//);
  543. # Disabled the utf8 one for now (JW) because it doesn't work on all perl distros
  544. # e.g. 5.6.1 works with or w/o 'use utf8' 5.6.0 fails w/o it. 5.005_03 
  545. # says "invalid hex value", etc. If anyone has a clue how to make this work 
  546. # please let me know!
  547. #  ($lines[0] =~ s/^∩╗┐//) || (eval('use utf8; $lines[0] =~ s/^\x{FEFF}//;'));
  548. #  $@ = ''; $! = undef;  # Clear any error messages
  549.  
  550.   
  551.   
  552.   # The first lines of the file must be blank, comments or start with [
  553.   my $first = '';
  554.   my $allCmt = $self->{allowed_comment_char};
  555.   foreach ( @lines ) {
  556.     next if /^\s*$/;    # ignore blank lines
  557.     next if /^\s*[$allCmt]/;    # ignore comments
  558.     $first = $_;
  559.     last;
  560.   }
  561.   unless( $first =~ /^\s*\[/ ) {
  562.     return undef;
  563.   }
  564.   
  565.   # Store what our line ending char was for output
  566.   ($self->{line_ends}) = $lines[0] =~ /([\015\012\025\n]+)/;
  567.   while ( @lines ) {
  568.     $_ = shift @lines;
  569.  
  570.     s/(\015\012?|\012|\025|\n)$//;                # remove line ending char(s)
  571.     $lineno++;
  572.     if (/^\s*$/) {                # ignore blank lines
  573.       next;
  574.     }
  575.     elsif (/^\s*[$allCmt]/) {            # collect comments
  576.       push(@cmts, $_);
  577.       next;
  578.     }
  579.     elsif (/^\s*\[\s*(\S|\S.*\S)\s*\]\s*$/) {        # New Section
  580.       $sect = $1;
  581.       if ($self->{nocase}) {
  582.         $sect = lc($sect);
  583.       }
  584.       $self->AddSection($sect);
  585.       $self->SetSectionComment($sect, @cmts);
  586.       @cmts = ();
  587.     }
  588.     elsif (($parm, $val) = /^\s*([^=]*?[^=\s])\s*=\s*(.*)$/) {    # new parameter
  589.       $parm = lc($parm) if $nocase;
  590.       $self->{pCMT}{$sect}{$parm} = [@cmts];
  591.       @cmts = ( );
  592.       if ($val =~ /^<<(.*)$/) {            # "here" value
  593.     my $eotmark  = $1;
  594.     my $foundeot = 0;
  595.     my $startline = $lineno;
  596.     my @val = ( );
  597.     while ( @lines ) {
  598.       $_ = shift @lines;
  599.       s/(\015\012?|\012|\025|\n)$//;                # remove line ending char(s)
  600.       $lineno++;
  601.       if ($_ eq $eotmark) {
  602.         $foundeot = 1;
  603.         last;
  604.       } else {
  605.         push(@val, $_);
  606.       }
  607.     }
  608.     if ($foundeot) {
  609.         if (exists $self->{v}{$sect}{$parm} && 
  610.             exists $loaded_params{$sect} && 
  611.             grep( /^\Q$parm\E$/, @{$loaded_params{$sect}}) ) {
  612.           if (ref($self->{v}{$sect}{$parm}) eq "ARRAY") {
  613.             # Add to the array
  614.             push @{$self->{v}{$sect}{$parm}}, @val;
  615.           } else {
  616.             # Create array
  617.             my $old_value = $self->{v}{$sect}{$parm};
  618.             my @new_value = ($old_value, @val);
  619.             $self->{v}{$sect}{$parm} = \@new_value;
  620.           }
  621.         } else {
  622.         $self->{v}{$sect}{$parm} = \@val;
  623.         $loaded_params{$sect} = [] unless $loaded_params{$sect};
  624.         push @{$loaded_params{$sect}}, $parm;
  625.         }
  626.         $self->{EOT}{$sect}{$parm} = $eotmark;
  627.     } else {
  628.       push(@Config::IniFiles::errors, sprintf('%d: %s', $startline,
  629.                   qq#no end marker ("$eotmark") found#));
  630.     }
  631.       } else { # no here value
  632.  
  633.         # process continuation lines, if any
  634.         while($self->{allowcontinue} && $val =~ s/\\$//) {
  635.           $_ = shift @lines;
  636.       s/(\015\012?|\012|\025|\n)$//; # remove line ending char(s)
  637.       $lineno++;
  638.           $val .= $_;
  639.         }
  640.  
  641.         # Now load value
  642.     if (exists $self->{v}{$sect}{$parm} &&
  643.         exists $loaded_params{$sect} && 
  644.         grep( /^\Q$parm\E$/, @{$loaded_params{$sect}}) ) {
  645.         if (ref($self->{v}{$sect}{$parm}) eq "ARRAY") {
  646.         # Add to the array
  647.         push @{$self->{v}{$sect}{$parm}}, $val;
  648.         } else {
  649.         # Create array
  650.         my $old_value = $self->{v}{$sect}{$parm};
  651.         my @new_value = ($old_value, $val);
  652.         $self->{v}{$sect}{$parm} = \@new_value;
  653.         }
  654.     } else {
  655.         $self->{v}{$sect}{$parm} = $val;
  656.         $loaded_params{$sect} = [] unless $loaded_params{$sect};
  657.         push @{$loaded_params{$sect}}, $parm;
  658.     }
  659.       }
  660.       push(@{$self->{parms}{$sect}}, $parm) unless grep(/^\Q$parm\E$/, @{$self->{parms}{$sect}});
  661.     }
  662.     else {
  663.       push(@Config::IniFiles::errors, sprintf("Line \%d in file " . $self->{cf} . " is mal-formed:\n\t\%s", $lineno, $_));
  664.     }
  665.   }
  666.  
  667.   #
  668.   # Now convert all the parameter hashes into tied hashes.
  669.   # This is in all uses, because it must be part of ReadConfig.
  670.   #
  671.   my %parms = %{$self->{startup_settings}};
  672.   if( defined $parms{-default} ) {
  673.     # If the default section doesn't exists, create it.
  674.     unless( defined $self->{v}{$parms{-default}} ) {
  675.       $self->{v}{$parms{-default}} = {};
  676.       push(@{$self->{sects}}, $parms{-default}) unless (grep /^\Q$parms{-default}\E$/, @{$self->{sects}});
  677.       $self->{parms}{$parms{-default}} = [];
  678.     } # end unless
  679.     $parms{-default} = $self->{v}{$parms{-default}};
  680.   } # end if
  681.   foreach( keys %{$self->{v}} ) {
  682.     $parms{-_current_value} = $self->{v}{$_};
  683.     $parms{-parms} = $self->{parms}{$_};
  684.     $self->{v}{$_} = {};
  685.     # Add a reference to our {parms} hash for each section
  686.     tie %{$self->{v}{$_}}, 'Config::IniFiles::_section', %parms
  687.   } # end foreach
  688.  
  689.   @Config::IniFiles::errors ? undef : 1;
  690. }
  691.  
  692.  
  693. =head2 Sections
  694.  
  695. Returns an array containing section names in the configuration file.
  696. If the I<nocase> option was turned on when the config object was
  697. created, the section names will be returned in lowercase.
  698.  
  699. =cut
  700.  
  701. sub Sections {
  702.   my $self = shift;
  703.   return @{$self->{sects}} if ref $self->{sects} eq 'ARRAY';
  704.   return ();
  705. }
  706.  
  707. =head2 SectionExists ( $sect_name )
  708.  
  709. Returns 1 if the specified section exists in the INI file, 0 otherwise (undefined if section_name is not defined).
  710.  
  711. =cut
  712.  
  713. sub SectionExists {
  714.     my $self = shift;
  715.     my $sect = shift;
  716.     
  717.     return undef if not defined $sect;
  718.     
  719.     if ($self->{nocase}) {
  720.         $sect = lc($sect);
  721.     }
  722.     
  723.     return undef() if not defined $sect;
  724.     return 1 if (grep {/^\Q$sect\E$/} @{$self->{sects}});
  725.     return 0;
  726. }
  727.  
  728. =head2 AddSection ( $sect_name )
  729.  
  730. Ensures that the named section exists in the INI file. If the section already
  731. exists, nothing is done. In this case, the "new" section will possibly contain
  732. data already.
  733.  
  734. If you really need to have a new section with no parameters in it, check that
  735. the name that you're adding isn't in the list of sections already.
  736.  
  737. =cut
  738.  
  739. sub AddSection {
  740.     my $self = shift;
  741.     my $sect = shift;
  742.     
  743.     return undef if not defined $sect;
  744.     
  745.     if ($self->{nocase}) {
  746.         $sect = lc($sect);
  747.     }
  748.     
  749.     return if $self->SectionExists($sect);
  750.     push @{$self->{sects}}, $sect;
  751.     $self->SetGroupMember($sect);
  752.     
  753.     # Set up the parameter names and values lists
  754.     $self->{parms}{$sect} = [] unless ref $self->{parms}{$sect} eq 'ARRAY';
  755.     if (!defined($self->{v}{$sect})) {
  756.         $self->{sCMT}{$sect} = [];
  757.         $self->{pCMT}{$sect} = {};        # Comments above parameters
  758.         $self->{parms}{$sect} = [];
  759.         $self->{v}{$sect} = {};
  760.     }
  761. }
  762.  
  763. =head2 DeleteSection ( $sect_name )
  764.  
  765. Completely removes the entire section from the configuration.
  766.  
  767. =cut
  768.  
  769. sub DeleteSection {
  770.     my $self = shift;
  771.     my $sect = shift;
  772.     
  773.     return undef if not defined $sect;
  774.     
  775.     if ($self->{nocase}) {
  776.         $sect = lc($sect);
  777.     }
  778.  
  779.     # This is done, the fast way, change if delval changes!!
  780.     delete $self->{v}{$sect};
  781.     delete $self->{sCMT}{$sect};
  782.     delete $self->{pCMT}{$sect};
  783.     delete $self->{EOT}{$sect};
  784.     delete $self->{parms}{$sect};
  785.  
  786.     @{$self->{sects}} = grep !/^\Q$sect\E$/, @{$self->{sects}};
  787.  
  788.     if( $sect =~ /^(\S+)\s+\S+/ ) {
  789.         my $group = $1;
  790.         if( defined($self->{group}{$group}) ) {
  791.             @{$self->{group}{$group}} = grep !/^\Q$sect\E$/, @{$self->{group}{$group}};
  792.         } # end if
  793.     } # end if
  794.  
  795.     return 1;
  796. } # end DeleteSection
  797.  
  798. =head2 Parameters ($sect_name)
  799.  
  800. Returns an array containing the parameters contained in the specified
  801. section.
  802.  
  803. =cut
  804.  
  805. sub Parameters {
  806.   my $self = shift;
  807.   my $sect = shift;
  808.   
  809.   return undef if not defined $sect;
  810.   
  811.   if ($self->{nocase}) {
  812.     $sect = lc($sect);
  813.   }
  814.   
  815.   return @{$self->{parms}{$sect}} if ref $self->{parms}{$sect} eq 'ARRAY';
  816.   return ();
  817. }
  818.  
  819. =head2 Groups
  820.  
  821. Returns an array containing the names of available groups.
  822.   
  823. Groups are specified in the config file as new sections of the form
  824.  
  825.   [GroupName MemberName]
  826.  
  827. This is useful for building up lists.  Note that parameters within a
  828. "member" section are referenced normally (i.e., the section name is
  829. still "Groupname Membername", including the space) - the concept of
  830. Groups is to aid people building more complex configuration files.
  831.  
  832. =cut
  833.  
  834. sub Groups    {
  835.   my $self = shift;
  836.   return keys %{$self->{group}} if ref $self->{group} eq 'HASH';
  837.   return ();
  838. }
  839.  
  840. =head2 SetGroupMember ( $sect )
  841.  
  842. Makes sure that the specified section is a member of the appropriate group.
  843.  
  844. Only intended for use in newval.
  845.  
  846. =cut
  847.  
  848. sub SetGroupMember {
  849.     my $self = shift;
  850.     my $sect = shift;
  851.     
  852.     return undef if not defined $sect;
  853.     
  854.     return(1) unless $sect =~ /^(\S+)\s+\S+/;
  855.     
  856.     my $group = $1;
  857.     if (not exists($self->{group}{$group})) {
  858.         $self->{group}{$group} = [];
  859.     }
  860.     if (not grep {/^\Q$sect\E$/} @{$self->{group}{$group}}) {
  861.         push @{$self->{group}{$group}}, $sect;
  862.     }
  863. }
  864.  
  865. =head2 RemoveGroupMember ( $sect )
  866.  
  867. Makes sure that the specified section is no longer a member of the
  868. appropriate group. Only intended for use in DeleteSection.
  869.  
  870. =cut
  871.  
  872. sub RemoveGroupMember {
  873.     my $self = shift;
  874.     my $sect = shift;
  875.     
  876.     return undef if not defined $sect;
  877.     
  878.     return(1) unless $sect =~ /^(\S+)\s+\S+/;
  879.     
  880.     my $group = $1;
  881.     return unless exists $self->{group}{$group};
  882.     @{$self->{group}{$group}} = grep {!/^\Q$sect\E$/} @{$self->{group}{$group}};
  883. }
  884.  
  885. =head2 GroupMembers ($group)
  886.  
  887. Returns an array containing the members of specified $group. Each element
  888. of the array is a section name. For example, given the sections
  889.  
  890.   [Group Element 1]
  891.   ...
  892.  
  893.   [Group Element 2]
  894.   ...
  895.  
  896. GroupMembers would return ("Group Element 1", "Group Element 2").
  897.  
  898. =cut
  899.  
  900. sub GroupMembers {
  901.   my $self  = shift;
  902.   my $group = shift;
  903.   
  904.   return undef if not defined $group;
  905.   
  906.   if ($self->{nocase}) {
  907.       $group = lc($group);
  908.   }
  909.   
  910.   return @{$self->{group}{$group}} if ref $self->{group}{$group} eq 'ARRAY';
  911.   return ();
  912. }
  913.  
  914. =head2 SetWriteMode ($mode)
  915.  
  916. Sets the mode (permissions) to use when writing the INI file.
  917.  
  918. $mode must be a string representation of the octal mode.
  919.  
  920. =cut
  921.  
  922. sub SetWriteMode
  923. {
  924.     my $self = shift;
  925.     my $mode = shift;
  926.     return undef if not defined ($mode);
  927.     return undef if not ($mode =~ m/[0-7]{3,3}/);
  928.     $self->{file_mode} = $mode;
  929.     return $mode;
  930. }
  931.  
  932. =head2 GetWriteMode ($mode)
  933.  
  934. Gets the current mode (permissions) to use when writing the INI file.
  935.  
  936. $mode is a string representation of the octal mode.
  937.  
  938. =cut
  939.  
  940. sub GetWriteMode
  941. {
  942.     my $self = shift;
  943.     return undef if not exists $self->{file_mode};
  944.     return $self->{file_mode};
  945. }
  946.  
  947. =head2 WriteConfig ($filename)
  948.  
  949. Writes out a new copy of the configuration file.  A temporary file
  950. (ending in '-new') is written out and then renamed to the specified
  951. filename.  Also see B<BUGS> below.
  952.  
  953. Returns true on success, C<undef> on failure.
  954.  
  955. =cut
  956.  
  957. sub WriteConfig {
  958.   my $self = shift;
  959.   my $file = shift;
  960.   
  961.   return undef unless defined $file;
  962.   
  963.   # If we are using a filename, then do mode checks and write to a 
  964.   # temporary file to avoid a race condition
  965.   if( !ref($file) ) {
  966.     if (-e $file) {
  967.           if (not (-w $file))
  968.           {
  969.                   #carp "File $file is not writable.  Refusing to write config";
  970.                   return undef;
  971.           }
  972.           my $mode = (stat $file)[2];
  973.           $self->{file_mode} = sprintf "%04o", ($mode & 0777);
  974.           #carp "Using mode $self->{file_mode} for file $file";
  975.     } elsif (defined($self->{file_mode}) and not (oct($self->{file_mode}) & 0222)) {
  976.           #carp "Store mode $self->{file_mode} prohibits writing config";
  977.     }
  978.   
  979.     my $new_file = $file . "-new";
  980.     local(*F);
  981.     open(F, "> $new_file") || do {
  982.       carp "Unable to write temp config file $new_file: $!";
  983.       return undef;
  984.     };
  985.     my $oldfh = select(F);
  986.     $self->OutputConfig;
  987.     close(F);
  988.     select($oldfh);
  989.     rename( $new_file, $file ) || do {
  990.       carp "Unable to rename temp config file ($new_file) to $file: $!";
  991.       return undef;
  992.     };
  993.     if (exists $self->{file_mode}) {
  994.       chmod oct($self->{file_mode}), $file;
  995.     }
  996.   
  997.   } # Otherwise, reset to the start of the file and write, unless we are using STDIN
  998.   else {
  999.     # Get a filehandle, allowing almost any type of 'file' parameter
  1000.     ## NB: If this were a filename, this would fail because _make_file 
  1001.     ##     opens a read-only handle, but we have already checked that case
  1002.     ##     so re-using the logic is ok [JW/WADG]
  1003.     my $fh = $self->_make_filehandle( $file );
  1004.     if (!$fh) {
  1005.       carp "Could not find a filehandle for the input stream ($file): $!";
  1006.       return undef;
  1007.     }
  1008.     
  1009.     
  1010.     # Only roll back if it's not STDIN (if it is, Carp)
  1011.     if( $fh == 0 ) {
  1012.       carp "Cannot write configuration file to STDIN.";
  1013.     } else {
  1014.       seek( $fh, 0, 0 );
  1015.       my $oldfh = select($fh);
  1016.       $self->OutputConfig;
  1017.       seek( $fh, 0, 0 );
  1018.       select($oldfh);
  1019.     } # end if
  1020.  
  1021.   } # end if (filehandle/name)
  1022.   
  1023.   return 1;
  1024.   
  1025. }
  1026.  
  1027. =head2 RewriteConfig
  1028.  
  1029. Same as WriteConfig, but specifies that the original configuration
  1030. file should be rewritten.
  1031.  
  1032. =cut
  1033.  
  1034. sub RewriteConfig {
  1035.   my $self = shift;
  1036.   
  1037.   return undef if (
  1038.     (not exists $self->{cf}) or
  1039.     (not defined $self->{cf}) or
  1040.     ($self->{cf} eq '')
  1041.   );
  1042.   
  1043.   # Return whatever WriteConfig returns :)
  1044.   $self->WriteConfig($self->{cf});
  1045. }
  1046.  
  1047. =head2 GetFileName
  1048.  
  1049. Returns the filename associated with this INI file.
  1050.  
  1051. If no filename has been specified, returns undef.
  1052.  
  1053. =cut
  1054.  
  1055. sub GetFileName
  1056. {
  1057.     my $self = shift;
  1058.     my $filename;
  1059.     if (exists $self->{cf}) {
  1060.         $filename = $self->{cf};
  1061.     } else {
  1062.         undef $filename;
  1063.     }
  1064.     return $filename;
  1065. }
  1066.  
  1067. =head2 SetFileName ($filename)
  1068.  
  1069. If you created the Config::IniFiles object without initialising from
  1070. a file, or if you just want to change the name of the file to use for
  1071. ReadConfig/RewriteConfig from now on, use this method.
  1072.  
  1073. Returns $filename if that was a valid name, undef otherwise.
  1074.  
  1075. =cut
  1076.  
  1077. sub SetFileName {
  1078.   my $self = shift;
  1079.   my $newfile = shift;
  1080.   
  1081.   return undef if not defined $newfile;
  1082.   
  1083.   if ($newfile ne "") {
  1084.     $self->{cf} = $newfile;
  1085.     return $self->{cf};
  1086.   }
  1087.   return undef;
  1088. }
  1089.  
  1090. # OutputConfig
  1091. #
  1092. # Writes OutputConfig to STDOUT. Use select() to redirect STDOUT to
  1093. # the output target before calling this function
  1094.  
  1095. sub OutputConfig {
  1096.   my $self = shift;
  1097.  
  1098.   my($sect, $parm, @cmts);
  1099.   my $ors = $self->{line_ends} || $\ || "\n";        # $\ is normally unset, but use input by default
  1100.   my $notfirst = 0;
  1101.   local $_;
  1102.   foreach $sect (@{$self->{sects}}) {
  1103.     next unless defined $self->{v}{$sect};
  1104.     print $ors if $notfirst;
  1105.     $notfirst = 1;
  1106.     if ((ref($self->{sCMT}{$sect}) eq 'ARRAY') &&
  1107.     (@cmts = @{$self->{sCMT}{$sect}})) {
  1108.       foreach (@cmts) {
  1109.     print "$_$ors";
  1110.       }
  1111.     }
  1112.     print "[$sect]$ors";
  1113.     next unless ref $self->{v}{$sect} eq 'HASH';
  1114.  
  1115.     foreach $parm (@{$self->{parms}{$sect}}) {
  1116.       if ((ref($self->{pCMT}{$sect}{$parm}) eq 'ARRAY') &&
  1117.       (@cmts = @{$self->{pCMT}{$sect}{$parm}})) {
  1118.     foreach (@cmts) {
  1119.       print "$_$ors";
  1120.     }
  1121.       }
  1122.  
  1123.       my $val = $self->{v}{$sect}{$parm};
  1124.       next if ! defined ($val);    # No parameter exists !!
  1125.       if (ref($val) eq 'ARRAY') {
  1126.         my $eotmark = $self->{EOT}{$sect}{$parm} || 'EOT';
  1127.     print "$parm= <<$eotmark$ors";
  1128.     foreach (@{$val}) {
  1129.       print "$_$ors";
  1130.     }
  1131.     print "$eotmark$ors";
  1132.       } elsif( $val =~ /[$ors]/ ) {
  1133.         # The FETCH of a tied hash is never called in 
  1134.         # an array context, so generate a EOT multiline
  1135.         # entry if the entry looks to be multiline
  1136.         my @val = split /[$ors]/, $val;
  1137.         if( @val > 1 ) {
  1138.           my $eotmark = $self->{EOT}{$sect}{$parm} || 'EOT';
  1139.           print "$parm= <<$eotmark$ors";
  1140.           print map "$_$ors", @val;
  1141.           print "$eotmark$ors";
  1142.         } else {
  1143.            print "$parm=$val[0]$ors";
  1144.         } # end if
  1145.       } else {
  1146.         print "$parm=$val$ors";
  1147.       }
  1148.     }
  1149.   }
  1150.   return 1;
  1151. }
  1152.  
  1153. =head2 SetSectionComment($section, @comment)
  1154.  
  1155. Sets the comment for section $section to the lines contained in @comment.
  1156.  
  1157. Each comment line will be prepended with the comment charcter (default
  1158. is C<#>) if it doesn't already have a comment character (ie: if the
  1159. line does not start with whitespace followed by an allowed comment
  1160. character, default is C<#> and C<;>).
  1161.  
  1162. To clear a section comment, use DeleteSectionComment ($section)
  1163.  
  1164. =cut
  1165.  
  1166. sub SetSectionComment
  1167. {
  1168.     my $self = shift;
  1169.     my $sect = shift;
  1170.     my @comment = @_;
  1171.  
  1172.     return undef if not defined $sect;
  1173.     return undef unless @comment;
  1174.     
  1175.     if ($self->{nocase}) {
  1176.         $sect = lc($sect);
  1177.     }
  1178.     
  1179.     $self->{sCMT}{$sect} = [];
  1180.     # At this point it's possible to have a comment for a section that
  1181.     # doesn't exist. This comment will not get written to the INI file.
  1182.     
  1183.     push @{$self->{sCMT}{$sect}}, $self->_markup_comments(@comment);
  1184.     return scalar @comment;
  1185. }
  1186.  
  1187.  
  1188.  
  1189. # this helper makes sure that each line is preceded with the correct comment
  1190. # character
  1191. sub _markup_comments 
  1192. {
  1193.   my $self = shift;
  1194.   my @comment = @_;
  1195.  
  1196.   my $allCmt = $self->{allowed_comment_char};
  1197.   my $cmtChr = $self->{comment_char};
  1198.   foreach (@comment) {
  1199.     m/^\s*[$allCmt]/ or ($_ = "$cmtChr $_");
  1200.   }
  1201.   @comment;
  1202. }
  1203.  
  1204.  
  1205.  
  1206. =head2 GetSectionComment ($section)
  1207.  
  1208. Returns a list of lines, being the comment attached to section $section. In 
  1209. scalar context, returns a string containing the lines of the comment separated
  1210. by newlines.
  1211.  
  1212. The lines are presented as-is, with whatever comment character was originally
  1213. used on that line.
  1214.  
  1215. =cut
  1216.  
  1217. sub GetSectionComment
  1218. {
  1219.     my $self = shift;
  1220.     my $sect = shift;
  1221.  
  1222.     return undef if not defined $sect;
  1223.     
  1224.     if ($self->{nocase}) {
  1225.         $sect = lc($sect);
  1226.     }
  1227.     
  1228.     if (exists $self->{sCMT}{$sect}) {
  1229.         return @{$self->{sCMT}{$sect}};
  1230.     } else {
  1231.         return undef;
  1232.     }
  1233. }
  1234.  
  1235. =head2 DeleteSectionComment ($section)
  1236.  
  1237. Removes the comment for the specified section.
  1238.  
  1239. =cut
  1240.  
  1241. sub DeleteSectionComment
  1242. {
  1243.     my $self = shift;
  1244.     my $sect = shift;
  1245.     
  1246.     return undef if not defined $sect;
  1247.     
  1248.     if ($self->{nocase}) {
  1249.         $sect = lc($sect);
  1250.     }
  1251.     
  1252.     delete $self->{sCMT}{$sect};
  1253. }
  1254.  
  1255. =head2 SetParameterComment ($section, $parameter, @comment)
  1256.  
  1257. Sets the comment attached to a particular parameter.
  1258.  
  1259. Any line of @comment that does not have a comment character will be
  1260. prepended with one. See L</SetSectionComment($section, @comment)> above
  1261.  
  1262. =cut
  1263.  
  1264. sub SetParameterComment
  1265. {
  1266.     my $self = shift;
  1267.     my $sect = shift;
  1268.     my $parm = shift;
  1269.     my @comment = @_;
  1270.  
  1271.     defined($sect) || return undef;
  1272.     defined($parm) || return undef;
  1273.     @comment || return undef;
  1274.     
  1275.     if ($self->{nocase}) {
  1276.         $sect = lc($sect);
  1277.         $parm = lc($parm);
  1278.     }
  1279.     
  1280.     if (not exists $self->{pCMT}{$sect}) {
  1281.         $self->{pCMT}{$sect} = {};
  1282.     }
  1283.     
  1284.     $self->{pCMT}{$sect}{$parm} = [];
  1285.     # Note that at this point, it's possible to have a comment for a parameter,
  1286.     # without that parameter actually existing in the INI file.
  1287.     push @{$self->{pCMT}{$sect}{$parm}}, $self->_markup_comments(@comment);
  1288.     return scalar @comment;
  1289. }
  1290.  
  1291. =head2 GetParameterComment ($section, $parameter)
  1292.  
  1293. Gets the comment attached to a parameter.
  1294.  
  1295. =cut
  1296.  
  1297. sub GetParameterComment
  1298. {
  1299.     my $self = shift;
  1300.     my $sect = shift;
  1301.     my $parm = shift;
  1302.     
  1303.     defined($sect) || return undef;
  1304.     defined($parm) || return undef;
  1305.     
  1306.     if ($self->{nocase}) {
  1307.         $sect = lc($sect);
  1308.         $parm = lc($parm);
  1309.     };
  1310.     
  1311.     exists($self->{pCMT}{$sect}) || return undef;
  1312.     exists($self->{pCMT}{$sect}{$parm}) || return undef;
  1313.     
  1314.     my @comment = @{$self->{pCMT}{$sect}{$parm}};
  1315.     return (wantarray)?@comment:join " ", @comment;
  1316. }
  1317.  
  1318. =head2 DeleteParameterComment ($section, $parmeter)
  1319.  
  1320. Deletes the comment attached to a parameter.
  1321.  
  1322. =cut
  1323.  
  1324. sub DeleteParameterComment
  1325. {
  1326.     my $self = shift;
  1327.     my $sect = shift;
  1328.     my $parm = shift;
  1329.     
  1330.     defined($sect) || return undef;
  1331.     defined($parm) || return undef;
  1332.     
  1333.     if ($self->{nocase}) {
  1334.         $sect = lc($sect);
  1335.         $parm = lc($parm);
  1336.     };
  1337.     
  1338.     # If the parameter doesn't exist, our goal has already been achieved
  1339.     exists($self->{pCMT}{$sect}) || return 1;
  1340.     exists($self->{pCMT}{$sect}{$parm}) || return 1;
  1341.     
  1342.     delete $self->{pCMT}{$sect}{$parm};
  1343.     return 1;
  1344. }
  1345.  
  1346. =head2 GetParameterEOT ($section, $parameter)
  1347.  
  1348. Accessor method for the EOT text (in fact, style) of the specified parameter. If any text is used as an EOT mark, this will be returned. If the parameter was not recorded using HERE style multiple lines, GetParameterEOT returns undef.
  1349.  
  1350. =cut
  1351.  
  1352. sub GetParameterEOT
  1353. {
  1354.     my $self = shift;
  1355.     my $sect = shift;
  1356.     my $parm = shift;
  1357.  
  1358.     defined($sect) || return undef;
  1359.     defined($parm) || return undef;
  1360.     
  1361.     if ($self->{nocase}) {
  1362.         $sect = lc($sect);
  1363.         $parm = lc($parm);
  1364.     };
  1365.  
  1366.     if (not exists $self->{EOT}{$sect}) {
  1367.         $self->{EOT}{$sect} = {};
  1368.     }
  1369.  
  1370.     if (not exists $self->{EOT}{$sect}{$parm}) {
  1371.         return undef;
  1372.     }
  1373.     return $self->{EOT}{$sect}{$parm};
  1374. }
  1375.  
  1376. =head2 SetParameterEOT ($section, $EOT)
  1377.  
  1378. Accessor method for the EOT text for the specified parameter. Sets the HERE style marker text to the value $EOT. Once the EOT text is set, that parameter will be saved in HERE style.
  1379.  
  1380. To un-set the EOT text, use DeleteParameterEOT ($section, $parameter).
  1381.  
  1382. =cut
  1383.  
  1384. sub SetParameterEOT
  1385. {
  1386.     my $self = shift;
  1387.     my $sect = shift;
  1388.     my $parm = shift;
  1389.     my $EOT = shift;
  1390.  
  1391.     defined($sect) || return undef;
  1392.     defined($parm) || return undef;
  1393.     defined($EOT) || return undef;
  1394.     
  1395.     if ($self->{nocase}) {
  1396.         $sect = lc($sect);
  1397.         $parm = lc($parm);
  1398.     };
  1399.  
  1400.     if (not exists $self->{EOT}{$sect}) {
  1401.         $self->{EOT}{$sect} = {};
  1402.     }
  1403.  
  1404.     $self->{EOT}{$sect}{$parm} = $EOT;
  1405. }
  1406.  
  1407. =head2 DeleteParameterEOT ($section, $parmeter)
  1408.  
  1409. Removes the EOT marker for the given section and parameter.
  1410. When writing a configuration file, if no EOT marker is defined 
  1411. then "EOT" is used.
  1412.  
  1413. =cut
  1414.  
  1415. sub DeleteParameterEOT
  1416. {
  1417.     my $self = shift;
  1418.     my $sect = shift;
  1419.     my $parm = shift;
  1420.     
  1421.     defined($sect) || return undef;
  1422.     defined($parm) || return undef;
  1423.     
  1424.     if ($self->{nocase}) {
  1425.         $sect = lc($sect);
  1426.         $parm = lc($parm);
  1427.     }
  1428.  
  1429.     delete $self->{EOT}{$sect}{$parm};
  1430. }
  1431.  
  1432.  
  1433. =head2 Delete
  1434.  
  1435. Deletes the entire configuration file in memory.
  1436.  
  1437. =cut
  1438.  
  1439. sub Delete {
  1440.     my $self = shift;
  1441.  
  1442.     # Again, done the fast way, if the data structure changes, change this!
  1443.     $self->{sects}  = [];
  1444.     $self->{parms}  = {};
  1445.     $self->{group}  = {};
  1446.     $self->{v}      = {};
  1447.     $self->{sCMT}   = {};
  1448.     $self->{pCMT}   = {};
  1449.     $self->{EOT}    = {};
  1450.  
  1451.     return 1;
  1452. } # end Delete
  1453.  
  1454.  
  1455.  
  1456. =head1 USAGE -- Tied Hash
  1457.  
  1458. =head2 tie %ini, 'Config::IniFiles', (-file=>$filename, [-option=>value ...] )
  1459.  
  1460. Using C<tie>, you can tie a hash to a B<Config::IniFiles> object. This creates a new
  1461. object which you can access through your hash, so you use this instead of the 
  1462. B<new> method. This actually creates a hash of hashes to access the values in 
  1463. the INI file. The options you provide through C<tie> are the same as given for 
  1464. the B<new> method, above.
  1465.  
  1466. Here's an example:
  1467.  
  1468.   use Config::IniFiles;
  1469.   
  1470.   my %ini
  1471.   tie %ini, 'Config::IniFiles', ( -file => "/path/configfile.ini" );
  1472.  
  1473.   print "We have $ini{Section}{Parameter}." if $ini{Section}{Parameter};
  1474.  
  1475. Accessing and using the hash works just like accessing a regular hash and 
  1476. many of the object methods are made available through the hash interface.
  1477.  
  1478. For those methods that do not coincide with the hash paradigm, you can use 
  1479. the Perl C<tied> function to get at the underlying object tied to the hash 
  1480. and call methods on that object. For example, to write the hash out to a new
  1481. ini file, you would do something like this:
  1482.  
  1483.   tied( %ini )->WriteConfig( "/newpath/newconfig.ini" ) ||
  1484.     die "Could not write settings to new file.";
  1485.  
  1486. =head2 $val = $ini{$section}{$parameter}
  1487.  
  1488. Returns the value of $parameter in $section. 
  1489.  
  1490. Because of limitations in Perl's tie implementation,
  1491. multiline values accessed through a hash will I<always> be returned 
  1492. as a single value with each line joined by the default line 
  1493. separator ($\). To break them apart you can simple do this:
  1494.  
  1495.   @lines = split( "$\", $ini{section}{multi_line_parameter} );
  1496.  
  1497. =head2 $ini{$section}{$parameter} = $value;
  1498.  
  1499. Sets the value of C<$parameter> in C<$section> to C<$value>. 
  1500.  
  1501. To set a multiline or multiv-alue parameter just assign an 
  1502. array reference to the hash entry, like this:
  1503.  
  1504.  $ini{$section}{$parameter} = [$value1, $value2, ...];
  1505.  
  1506. If the parameter did not exist in the original file, it will 
  1507. be created. However, Perl does not seem to extend autovivification 
  1508. to tied hashes. That means that if you try to say
  1509.  
  1510.   $ini{new_section}{new_paramters} = $val;
  1511.  
  1512. and the section 'new_section' does not exist, then Perl won't 
  1513. properly create it. In order to work around this you will need 
  1514. to create a hash reference in that section and then assign the
  1515. parameter value. Something like this should do nicely:
  1516.  
  1517.   $ini{new_section} = {};
  1518.   $ini{new_section}{new_paramters} = $val;
  1519.  
  1520. =head2 %hash = %{$ini{$section}}
  1521.  
  1522. Using the tie interface, you can copy whole sections of the 
  1523. ini file into another hash. Note that this makes a copy of 
  1524. the entire section. The new hash in no longer tied to the 
  1525. ini file, In particular, this means -default and -nocase 
  1526. settings will not apply to C<%hash>.
  1527.  
  1528.  
  1529. =head2 $ini{$section} = {}; %{$ini{$section}} = %parameters;
  1530.  
  1531. Through the hash interface, you have the ability to replace 
  1532. the entire section with a new set of parameters. This call
  1533. will fail, however, if the argument passed in NOT a hash 
  1534. reference. You must use both lines, as shown above so that 
  1535. Perl recognizes the section as a hash reference context 
  1536. before COPYing over the values from your C<%parameters> hash.
  1537.  
  1538. =head2 delete $ini{$section}{$parameter}
  1539.  
  1540. When tied to a hash, you can use the Perl C<delete> function
  1541. to completely remove a parameter from a section.
  1542.  
  1543. =head2 delete $ini{$section}
  1544.  
  1545. The tied interface also allows you to delete an entire 
  1546. section from the ini file using the Perl C<delete> function.
  1547.  
  1548. =head2 %ini = ();
  1549.  
  1550. If you really want to delete B<all> the items in the ini file, this 
  1551. will do it. Of course, the changes won't be written to the actual
  1552. file unless you call B<RewriteConfig> on the object tied to the hash.
  1553.  
  1554. =head2 Parameter names
  1555.  
  1556. =over 4
  1557.  
  1558. =item my @keys = keys %{$ini{$section}}
  1559.  
  1560. =item while (($k, $v) = each %{$ini{$section}}) {...}
  1561.  
  1562. =item if( exists %{$ini{$section}}, $parameter ) {...}
  1563.  
  1564. =back
  1565.  
  1566. When tied to a hash, you use the Perl C<keys> and C<each> 
  1567. functions to iteratively list the parameters (C<keys>) or 
  1568. parameters and their values (C<each>) in a given section.
  1569.  
  1570. You can also use the Perl C<exists> function to see if a 
  1571. parameter is defined in a given section.
  1572.  
  1573. Note that none of these will return parameter names that 
  1574. are part if the default section (if set), although accessing
  1575. an unknown parameter in the specified section will return a 
  1576. value from the default section if there is one.
  1577.  
  1578.  
  1579. =head2 Section names
  1580.  
  1581. =over 4
  1582.  
  1583. =item foreach( keys %ini ) {...}
  1584.  
  1585. =item while (($k, $v) = each %ini) {...}
  1586.  
  1587. =item if( exists %ini, $section ) {...}
  1588.  
  1589. =back
  1590.  
  1591. When tied to a hash, you use the Perl C<keys> and C<each> 
  1592. functions to iteratively list the sections in the ini file.
  1593.  
  1594. You can also use the Perl C<exists> function to see if a 
  1595. section is defined in the file.
  1596.  
  1597. =cut
  1598.  
  1599. ############################################################
  1600. #
  1601. # TIEHASH Methods
  1602. #
  1603. # Description:
  1604. # These methods allow you to tie a hash to the 
  1605. # Config::IniFiles object. Note that, when tied, the 
  1606. # user wants to look at thinks like $ini{sec}{parm}, but the 
  1607. # TIEHASH only provides one level of hash interace, so the 
  1608. # root object gets asked for a $ini{sec}, which this 
  1609. # implements. To further tie the {parm} hash, the internal 
  1610. # class Config::IniFiles::_section, is provided, below.
  1611. #
  1612. ############################################################
  1613. # ----------------------------------------------------------
  1614. # Date      Modification                              Author
  1615. # ----------------------------------------------------------
  1616. # 2000May09 Created method                                JW
  1617. # ----------------------------------------------------------
  1618. sub TIEHASH {
  1619.   my $class = shift;
  1620.   my %parms = @_;
  1621.  
  1622.   # Get a new object
  1623.   my $self = $class->new( %parms );
  1624.  
  1625.   return $self;
  1626. } # end TIEHASH
  1627.  
  1628.  
  1629. # ----------------------------------------------------------
  1630. # Date      Modification                              Author
  1631. # ----------------------------------------------------------
  1632. # 2000May09 Created method                                JW
  1633. # ----------------------------------------------------------
  1634. sub FETCH {
  1635.   my $self = shift;
  1636.   my( $key ) = @_;
  1637.  
  1638.   $key = lc($key) if( $self->{nocase} );
  1639.  
  1640.   return $self->{v}{$key};
  1641. } # end FETCH
  1642.  
  1643. # ----------------------------------------------------------
  1644. # Date      Modification                              Author
  1645. # ----------------------------------------------------------
  1646. # 2000Jun14 Fixed bug where wrong ref was saved           JW
  1647. # 2000Oct09 Fixed possible but in %parms with defaults    JW
  1648. # 2001Apr04 Fixed -nocase problem in storing              JW
  1649. # ----------------------------------------------------------
  1650. sub STORE {
  1651.   my $self = shift;
  1652.   my( $key, $ref ) = @_;
  1653.  
  1654.   return undef unless ref($ref) eq 'HASH';
  1655.  
  1656.   $key = lc($key) if( $self->{nocase} );
  1657.  
  1658.   # Create a new hash and tie it to a _sections object with the ref's data
  1659.   $self->{v}{$key} = {};
  1660.  
  1661.   # Store the section name in the list
  1662.   push(@{$self->{sects}}, $key) unless (grep {/^\Q$key\E$/} @{$self->{sects}});
  1663.  
  1664.   my %parms = %{$self->{startup_settings}};
  1665.   $self->{parms}{$key} = [];
  1666.   $parms{-parms} = $self->{parms}{$key};
  1667.   $parms{-_current_value} = $ref;
  1668.   delete $parms{default};
  1669.   $parms{-default} = $self->{v}{$parms{-default}} if defined $parms{-default} && defined $self->{v}{$parms{-default}};
  1670.   tie %{$self->{v}{$key}}, 'Config::IniFiles::_section', %parms;
  1671. } # end STORE
  1672.  
  1673.  
  1674. # ----------------------------------------------------------
  1675. # Date      Modification                              Author
  1676. # ----------------------------------------------------------
  1677. # 2000May09 Created method                                JW
  1678. # 2000Dec17 Now removes comments, groups and EOTs too     JW
  1679. # 2001Arp04 Fixed -nocase problem                         JW
  1680. # ----------------------------------------------------------
  1681. sub DELETE {
  1682.   my $self = shift;
  1683.   my( $key ) = @_;
  1684.  
  1685.   $key = lc($key) if( $self->{nocase} );
  1686.  
  1687.   delete $self->{sCMT}{$key};
  1688.   delete $self->{pCMT}{$key};
  1689.   delete $self->{EOT}{$key};
  1690.   delete $self->{parms}{$key};
  1691.  
  1692.   if( $key =~ /(\S+)\s+\S+/ ) {
  1693.     my $group = $1;
  1694.     if( defined($self->{group}{$group}) ) {
  1695.       @{$self->{group}{$group}} = grep !/\Q$key\E/, @{$self->{group}{$group}};
  1696.     } # end if
  1697.   } # end if
  1698.  
  1699.   @{$self->{sects}} = grep !/^\Q$key\E$/, @{$self->{sects}};
  1700.   return delete( $self->{v}{$key} );
  1701. } # end DELETE
  1702.  
  1703.  
  1704. # ----------------------------------------------------------
  1705. # Date      Modification                              Author
  1706. # ----------------------------------------------------------
  1707. # 2000May09 Created method                                JW
  1708. # ----------------------------------------------------------
  1709. sub CLEAR {
  1710.   my $self = shift;
  1711.  
  1712.   foreach (keys %{$self->{v}}) {
  1713.      $self->DELETE( $_ );
  1714.   } # end foreach
  1715.  
  1716. } # end CLEAR
  1717.  
  1718. # ----------------------------------------------------------
  1719. # Date      Modification                              Author
  1720. # ----------------------------------------------------------
  1721. # 2000May09 Created method                                JW
  1722. # ----------------------------------------------------------
  1723. sub FIRSTKEY {
  1724.   my $self = shift;
  1725.  
  1726.   my $a = keys %{$self->{v}};
  1727.   return each %{$self->{v}};
  1728. } # end FIRSTKEY
  1729.  
  1730.  
  1731. # ----------------------------------------------------------
  1732. # Date      Modification                              Author
  1733. # ----------------------------------------------------------
  1734. # 2000May09 Created method                                JW
  1735. # ----------------------------------------------------------
  1736. sub NEXTKEY {
  1737.   my $self = shift;
  1738.   my( $last ) = @_;
  1739.  
  1740.   return each %{$self->{v}};
  1741. } # end NEXTKEY
  1742.  
  1743.  
  1744. # ----------------------------------------------------------
  1745. # Date      Modification                              Author
  1746. # ----------------------------------------------------------
  1747. # 2000May09 Created method                                JW
  1748. # 2001Apr04 Fixed -nocase bug and false true bug          JW
  1749. # ----------------------------------------------------------
  1750. sub EXISTS {
  1751.   my $self = shift;
  1752.   my( $key ) = @_;
  1753.   $key = lc($key) if( $self->{nocase} );
  1754.  
  1755.   return exists $self->{v}{$key};
  1756. } # end EXISTS
  1757.  
  1758.  
  1759. # ----------------------------------------------------------
  1760. # DESTROY is used by TIEHASH and the Perl garbage collector,
  1761. # ----------------------------------------------------------
  1762. # Date      Modification                              Author
  1763. # ----------------------------------------------------------
  1764. # 2000May09 Created method                                JW
  1765. # ----------------------------------------------------------
  1766. sub DESTROY {
  1767.   # my $self = shift;
  1768. } # end if
  1769.  
  1770.  
  1771. # ----------------------------------------------------------
  1772. # Sub: _make_filehandle
  1773. #
  1774. # Args: $thing
  1775. #    $thing    An input source
  1776. #
  1777. # Description: Takes an input source of a filehandle, 
  1778. # filehandle glob, reference to a filehandle glob, IO::File
  1779. # object or scalar filename and returns a file handle to 
  1780. # read from it with.
  1781. # ----------------------------------------------------------
  1782. # Date      Modification                              Author
  1783. # ----------------------------------------------------------
  1784. # 06Dec2001 Added to support input from any source        JW
  1785. # ----------------------------------------------------------
  1786. sub _make_filehandle {
  1787.   my $self = shift;
  1788.  
  1789.   #
  1790.   # This code is 'borrowed' from Lincoln D. Stein's GD.pm module
  1791.   # with modification for this module. Thanks Lincoln!
  1792.   #
  1793.   
  1794.   no strict 'refs';
  1795.   my $thing = shift;
  1796.   return $thing if defined(fileno $thing);
  1797. #  return $thing if defined($thing) && ref($thing) && defined(fileno $thing);
  1798.   
  1799.   # otherwise try qualifying it into caller's package
  1800.   my $fh = qualify_to_ref($thing,caller(1));
  1801.   return $fh if defined(fileno $fh);
  1802. #  return $fh if defined($thing) && ref($thing) && defined(fileno $fh);
  1803.   
  1804.   # otherwise treat it as a file to open
  1805.   $fh = gensym;
  1806.   open($fh,$thing) || return;
  1807.   
  1808.   return $fh;
  1809. } # end _make_filehandle
  1810.  
  1811.  
  1812.  
  1813. ############################################################
  1814. #
  1815. # INTERNAL PACKAGE: Config::IniFiles::_section
  1816. #
  1817. # Description:
  1818. # This package is used to provide a single-level TIEHASH
  1819. # interface to the sections in the IniFile. When tied, the 
  1820. # user wants to look at thinks like $ini{sec}{parm}, but the 
  1821. # TIEHASH only provides one level of hash interace, so the 
  1822. # root object gets asked for a $ini{sec} and must return a 
  1823. # has reference that accurately covers the '{parm}' part.
  1824. #
  1825. # This package is only used when tied and is inter-woven 
  1826. # between the sections and their parameters when the TIEHASH
  1827. # method is called by Perl. It's a very simple implementation
  1828. # of a tied hash object with support for the Config::IniFiles
  1829. # -nocase and -default options.
  1830. #
  1831. ############################################################
  1832. # Date        Modification                            Author
  1833. # ----------------------------------------------------------
  1834. # 2000.May.09 Created to excapsulate TIEHASH interface    JW
  1835. ############################################################
  1836. package Config::IniFiles::_section;
  1837.  
  1838. use strict;
  1839. use Carp;
  1840. use vars qw( $VERSION );
  1841.  
  1842. $Config::IniFiles::_section::VERSION = 2.16;
  1843.  
  1844. # ----------------------------------------------------------
  1845. # Sub: Config::IniFiles::_section::TIEHASH
  1846. #
  1847. # Args: $class, %parms
  1848. #    $class    The class that this is being tied to.
  1849. #    %parms   Contains named parameters passed from the 
  1850. #           constructor plus thes parameters
  1851. #    -_current_value    holds the values to be inserted in the hash.
  1852. #    -default    should be a hash ref.
  1853. #    -parms      reference to the $self->{parms}{$sect} of the parent
  1854. #
  1855. # Description: Builds the object that gets tied to the 
  1856. # sections name. Inserts the existing hash, defined in the 
  1857. # named parameter '-_current_value' into the tied hash.
  1858. # ----------------------------------------------------------
  1859. # Date      Modification                              Author
  1860. # ----------------------------------------------------------
  1861. # ----------------------------------------------------------
  1862. sub TIEHASH {
  1863.   my $proto = shift;
  1864.   my $class = ref($proto) || $proto;
  1865.   my %parms = @_;
  1866.   
  1867.   # Make a new object
  1868.   my $self = {};
  1869.   
  1870.   # Put the passed hash into the holder
  1871.   $self->{v} = $parms{-_current_value};
  1872.   
  1873.   # Get all other the parms, removing leading '-', if any
  1874.   # Option checking is already handled in the Config::IniFiles contructor
  1875.   foreach( keys %parms ) {
  1876.     s/^-//g;
  1877.     $self->{$_} = $parms{-$_};
  1878.   } # end foreach
  1879.  
  1880.   return bless( $self, $class );
  1881. } # end TIEHASH
  1882.  
  1883.  
  1884. # ----------------------------------------------------------
  1885. # Sub: Config::IniFiles::_section::FETCH
  1886. #
  1887. # Args: $key
  1888. #    $key    The name of the key whose value to get
  1889. #
  1890. # Description: Returns the value associated with $key. If
  1891. # the value is a hash, returns a hashref, just like normal
  1892. # Perl hashes.
  1893. # ----------------------------------------------------------
  1894. # Date      Modification                              Author
  1895. # ----------------------------------------------------------
  1896. # 2000Jun15 Fixed bugs in -default handler                JW
  1897. # 2000Dec07 Fixed another bug in -deault handler          JW
  1898. # 2002Jul04 Returning scalar values (Bug:447532)          AS
  1899. # ----------------------------------------------------------
  1900. sub FETCH {
  1901.   my $self = shift;
  1902.   my $key = shift;
  1903.  
  1904.   $key = lc($key) if( $self->{nocase} );
  1905.  
  1906.   my $val = $self->{v}{$key};
  1907.   
  1908.   unless( defined $self->{v}{$key} ) {
  1909.     $val = $self->{default}{$key} if ref($self->{default}) eq 'HASH';
  1910.   } # end unless
  1911.  
  1912.   return $val;
  1913. } # end FETCH
  1914.  
  1915.  
  1916. # ----------------------------------------------------------
  1917. # Sub: Config::IniFiles::_section::STORE
  1918. #
  1919. # Args: $key, @val
  1920. #    $key    The key under which to store the value
  1921. #    @val    The value to store, either an array or a scalar
  1922. #
  1923. # Description: Sets the value for the specified $key
  1924. # ----------------------------------------------------------
  1925. # Date      Modification                              Author
  1926. # ----------------------------------------------------------
  1927. # 2001Apr04 Fixed -nocase bug                             JW
  1928. # ----------------------------------------------------------
  1929. sub STORE {
  1930.   my $self = shift;
  1931.   my $key = shift;
  1932.   my @val = @_;
  1933.  
  1934.   $key = lc($key) if( $self->{nocase} );
  1935.  
  1936.   # Add the parameter the the parent's list if it isn't there yet
  1937.   push(@{$self->{parms}}, $key) unless (grep /^\Q$key\E$/, @{$self->{parms}});
  1938.  
  1939.   if (@val > 1) {
  1940.     $self->{v}{$key} = @val;
  1941.   } else {
  1942.     $self->{v}{$key} = shift @val;
  1943.   }
  1944.  
  1945.   return $self->{v}{$key};
  1946. } # end STORE
  1947.  
  1948.  
  1949. # ----------------------------------------------------------
  1950. # Sub: Config::IniFiles::_section::DELETE
  1951. #
  1952. # Args: $key
  1953. #    $key    The key to remove from the hash
  1954. #
  1955. # Description: Removes the specified key from the hash
  1956. # ----------------------------------------------------------
  1957. # Date      Modification                              Author
  1958. # ----------------------------------------------------------
  1959. # 2001Apr04 Fixed -nocase bug                              JW
  1960. # ----------------------------------------------------------
  1961. sub DELETE   {
  1962.   my $self = shift;
  1963.   my $key = shift;
  1964.  
  1965.   $key = lc($key) if( $self->{nocase} );
  1966. #    @{$self->{parms}{$sect}} = grep !/^$parm$/, @{$self->{parms}{$sect}};
  1967.   return delete $self->{v}{$key};
  1968. } # end DELETE
  1969.  
  1970. # ----------------------------------------------------------
  1971. # Sub: Config::IniFiles::_section::CLEAR
  1972. #
  1973. # Args: (None)
  1974. #
  1975. # Description: Empties the entire hash
  1976. # ----------------------------------------------------------
  1977. # Date      Modification                              Author
  1978. # ----------------------------------------------------------
  1979. # ----------------------------------------------------------
  1980. sub CLEAR    {
  1981.   my $self = shift;
  1982.  
  1983.   foreach ( keys %{$self->{v}}) {
  1984.     $self->DELETE($_);
  1985.   } # end foreach
  1986.  
  1987.   return $self;
  1988. } # end CLEAR
  1989.  
  1990. # ----------------------------------------------------------
  1991. # Sub: Config::IniFiles::_section::EXISTS
  1992. #
  1993. # Args: $key
  1994. #    $key    The key to look for
  1995. #
  1996. # Description: Returns whether the key exists
  1997. # ----------------------------------------------------------
  1998. # Date      Modification                              Author
  1999. # ----------------------------------------------------------
  2000. # 2001Apr04 Fixed -nocase bug                             JW
  2001. # ----------------------------------------------------------
  2002. sub EXISTS   {
  2003.   my $self = shift;
  2004.   my $key = shift;
  2005.   $key = lc($key) if( $self->{nocase} );
  2006.   return exists $self->{v}{$key};
  2007. } # end EXISTS
  2008.  
  2009. # ----------------------------------------------------------
  2010. # Sub: Config::IniFiles::_section::FIRSTKEY
  2011. #
  2012. # Args: (None)
  2013. #
  2014. # Description: Returns the first key in the hash
  2015. # ----------------------------------------------------------
  2016. # Date      Modification                              Author
  2017. # ----------------------------------------------------------
  2018. # ----------------------------------------------------------
  2019. sub FIRSTKEY {
  2020.   my $self = shift;
  2021.  
  2022.   # Reset the each() iterator
  2023.   my $a = keys %{$self->{v}};
  2024.  
  2025.   return each %{$self->{v}};
  2026. } # end FIRST KEY
  2027.  
  2028. # ----------------------------------------------------------
  2029. # Sub: Config::IniFiles::_section::NEXTKEY
  2030. #
  2031. # Args: $last
  2032. #    $last    The last key accessed by the interator
  2033. #
  2034. # Description: Returns the next key in line
  2035. # ----------------------------------------------------------
  2036. # Date      Modification                              Author
  2037. # ----------------------------------------------------------
  2038. # ----------------------------------------------------------
  2039. sub NEXTKEY  {
  2040.   my $self = shift;
  2041.   my $last = shift;
  2042.  
  2043.   return each %{$self->{v}};
  2044. } # end NEXTKEY
  2045.  
  2046.  
  2047. # ----------------------------------------------------------
  2048. # Sub: Config::IniFiles::_section::DESTROY
  2049. #
  2050. # Args: (None)
  2051. #
  2052. # Description: Called on cleanup
  2053. # ----------------------------------------------------------
  2054. # Date      Modification                              Author
  2055. # ----------------------------------------------------------
  2056. # ----------------------------------------------------------
  2057. sub DESTROY  {
  2058.   # my $self = shift
  2059. } # end DESTROY
  2060.  
  2061. # Eliminate annoying warnings
  2062. if ($^W)    {
  2063.     $Config::IniFiles::VERSION = $Config::IniFiles::VERSION;
  2064. }
  2065.  
  2066. 1;
  2067.  
  2068. =head1 DIAGNOSTICS
  2069.  
  2070. =head2 @Config::IniFiles::errors
  2071.  
  2072. Contains a list of errors encountered while parsing the configuration
  2073. file.  If the I<new> method returns B<undef>, check the value of this
  2074. to find out what's wrong.  This value is reset each time a config file
  2075. is read.
  2076.  
  2077. =head1 BUGS
  2078.  
  2079. =over 3
  2080.  
  2081. =item *
  2082.  
  2083. The output from [Re]WriteConfig/OutputConfig might not be as pretty as
  2084. it can be.  Comments are tied to whatever was immediately below them.
  2085. And case is not preserved for Section and Parameter names if the -nocase
  2086. option was used.
  2087.  
  2088. =item *
  2089.  
  2090. No locking is done by [Re]WriteConfig.  When writing servers, take
  2091. care that only the parent ever calls this, and consider making your
  2092. own backup.
  2093.  
  2094. =back
  2095.  
  2096. =head1 Data Structure
  2097.  
  2098. Note that this is only a reference for the package maintainers - one of the
  2099. upcoming revisions to this package will include a total clean up of the
  2100. data structure.
  2101.  
  2102.   $iniconf->{cf} = "config_file_name"
  2103.           ->{startup_settings} = \%orginal_object_parameters
  2104.           ->{firstload} = 0
  2105.           ->{nocase} = 0
  2106.           ->{reloadwarn} = 0
  2107.           ->{sects} = \@sections
  2108.           ->{sCMT}{$sect} = \@comment_lines
  2109.           ->{group}{$group} = \@group_members
  2110.           ->{parms}{$sect} = \@section_parms
  2111.           ->{EOT}{$sect}{$parm} = "end of text string"
  2112.           ->{pCMT}{$sect}{$parm} = \@comment_lines
  2113.           ->{v}{$sect}{$parm} = $value   OR  \@values
  2114.  
  2115. =head1 AUTHOR and ACKNOWLEDGEMENTS
  2116.  
  2117. The original code was written by Scott Hutton.
  2118. Then handled for a time by Rich Bowen (thanks!),
  2119. It is now managed by Jeremy Wadsack,
  2120. with many contributions from various other people.
  2121.  
  2122. In particular, special thanks go to (in roughly chronological order):
  2123.  
  2124. Bernie Cosell, Alan Young, Alex Satrapa, Mike Blazer, Wilbert van de Pieterman,
  2125. Steve Campbell, Robert Konigsberg, Scott Dellinger, R. Bernstein,
  2126. Daniel Winkelmann, Pires Claudio, Adrian Phillips, 
  2127. Marek Rouchal, Luc St Louis, Adam Fischler, Kay R÷pke, Matt Wilson, 
  2128. Raviraj Murdeshwar and Slaven Rezic, Florian Pfaff
  2129.  
  2130. Geez, that's a lot of people. And apologies to the folks who were missed.
  2131.  
  2132. If you want someone to bug about this, that would be:
  2133.  
  2134.     Jeremy Wadsack <dgsupport at wadsack-allen dot com>
  2135.  
  2136. If you want more information, or want to participate, go to:
  2137.  
  2138.     http://sourceforge.net/projects/config-inifiles/
  2139.  
  2140. Please send bug reports to config-inifiles-bugs@lists.sourceforge.net
  2141.  
  2142. Development discussion occurs on the mailing list
  2143. config-inifiles-dev@lists.sourceforge.net, which you can subscribe
  2144. to by going to the project web site (link above).
  2145.  
  2146. This program is free software; you can redistribute it and/or 
  2147. modify it under the same terms as Perl itself.
  2148.  
  2149. =head1 Change log
  2150.  
  2151.      $Log: IniFiles.pm,v $
  2152.      Revision 2.38  2003/05/14 01:30:32  wadg
  2153.      - fixed RewriteConfig and ReadConfig to work with open file handles
  2154.      - added a test to ensure that blank files throw no warnings
  2155.      - added a test for error messages from malformed lines
  2156.  
  2157.      Revision 2.37  2003/01/31 23:00:35  wadg
  2158.      Updated t/07misc test 4 to remove warning
  2159.  
  2160.      Revision 2.36  2002/12/18 01:43:11  wadg
  2161.      - Improved error message when an invalid line is encountered in INI file
  2162.      - Fixed bug 649220; importing a non-file-based object into a file one
  2163.        no longer destroys the original object
  2164.  
  2165.      Revision 2.33  2002/11/12 14:48:16  grail
  2166.      Addresses feature request - [ 403496 ] A simple change will allow support on more platforms
  2167.  
  2168.      Revision 2.32  2002/11/12 14:15:44  grail
  2169.      Addresses bug - [225971] Respect Read-Only Permissions of File System
  2170.  
  2171.      Revision 2.31  2002/10/29 01:45:47  grail
  2172.      [ 540867 ] Add GetFileName method
  2173.  
  2174.      Revision 2.30  2002/10/15 18:51:07  wadg
  2175.      Patched to stopwarnings about utf8 usage.
  2176.  
  2177.      Revision 2.29  2002/08/15 21:33:58  wadg
  2178.      - Support for UTF Byte-Order-Mark (Raviraj Murdeshwar)
  2179.      - Made tests portable to Mac (p. kent)
  2180.      - Made file parsing portable for s390/EBCDIC, etc. (Adam Fischler)
  2181.      - Fixed import bug with Perl 5.8.0 (Marek Rouchal)
  2182.      - Fixed precedence bug in WriteConfig (Luc St Louis)
  2183.      - Fixed broken group detection in SetGroupMember and RemoveGroupMember (Kay R÷pke)
  2184.      - Added line continuation character (/) support (Marek Rouchal)
  2185.      - Added configurable comment character support (Marek Rouchal)
  2186.  
  2187.      Revision 2.28  2002/07/04 03:56:05  grail
  2188.      Changes for resolving bug 447532 - _section::FETCH should return array ref for multiline values.
  2189.  
  2190.      Revision 2.27  2001/12/20 16:03:49  wadg
  2191.      - Fixed bug introduced in new valid file check where ';' comments in first lines were not considered valid
  2192.      - Rearranged some tests to put them in the proper files (case and -default)
  2193.      - Added more comment test to cover more cases
  2194.      - Fixed first two comments tests which weren't doing anything
  2195.  
  2196.      Revision 2.26  2001/12/19 22:20:50  wadg
  2197.      #481513 Recognize badly formatted files
  2198.  
  2199.      Revision 2.25  2001/12/12 20:44:48  wadg
  2200.      Update to bring CVS version in synch
  2201.  
  2202.      Revision 2.24  2001/12/07 10:03:06  wadg
  2203.      222444 Ability to load from arbitrary source
  2204.  
  2205.      Revision 2.23  2001/12/07 09:35:06  wadg
  2206.      Forgot to include updates t/test.ini
  2207.  
  2208.      Revision 2.22  2001/12/06 16:52:39  wadg
  2209.      Fixed bugs 482353,233372. Updated doc for new mgr.
  2210.  
  2211.      Revision 2.21  2001/08/14 01:49:06  wadg
  2212.      Bug fix: multiple blank lines counted as one
  2213.      Patched README change log to include recent updates
  2214.  
  2215.      Revision 2.20  2001/06/07 02:49:52  grail
  2216.       - Added checks for method parameters being defined
  2217.       - fixed some regexes to make them stricter
  2218.       - Fixed greps to make them consistent through the code (also a vain
  2219.         attempt to help my editors do syntax colouring properly)
  2220.       - Added AddSection method, replaced chunk of ReadConfig with AddSection
  2221.       - Added case handling stuff to more methods
  2222.       - Added RemoveGroupMember
  2223.       - Made variable names more consistent through OO methods
  2224.       - Restored Unix EOLs
  2225.  
  2226.      Revision 2.19  2001/04/04 23:33:40  wadg
  2227.      Fixed case sensitivity bug
  2228.  
  2229.      Revision 2.18  2001/03/30 04:41:08  rbowen
  2230.      Small documentation change in IniFiles.pm - pod2* was choking on misplaces
  2231.      =item tags. And I regenerated the README
  2232.      The main reason for this release is that the MANIFEST in the 2.17 version was
  2233.      missing one of the new test suite files, and that is included in this
  2234.      re-release.
  2235.  
  2236.      Revision 2.17  2001/03/21 21:05:12  wadg
  2237.      Documentation edits
  2238.  
  2239.      Revision 2.16  2001/03/21 19:59:09 wadg
  2240.      410327 -default not in original; 233255 substring parameters
  2241.  
  2242.      Revision 2.15  2001/01/30 11:46:48  rbowen
  2243.      Very minor documentation bug fixed.
  2244.  
  2245.      Revision 2.14  2001/01/08 18:02:32  wadg
  2246.      [Bug #127325] Fixed proken import; changelog; moved
  2247.  
  2248.      Revision 2.13  2000/12/18 07:14:41  wadg
  2249.      [Bugs# 122441,122437] Alien EOLs and OO delete method
  2250.  
  2251.      Revision 2.12  2000/12/18 04:59:37  wadg
  2252.      [Bug #125524] Writing multiline of 2 with tied hash
  2253.  
  2254.      Revision 2.11  2000/12/16 12:53:13  grail
  2255.      [BUG #122455] Problem with File Permissions
  2256.  
  2257.      Revision 2.10  2000/12/13 17:40:18  rbowen
  2258.      Updated version number so that CPAN will stop being angry with us.
  2259.  
  2260.      Revision 1.18  2000/12/08 00:45:35  grail
  2261.      Change as requested by Jeremy Wadsack, for Bug 123146
  2262.  
  2263.      Revision 1.17  2000/12/07 15:32:36  grail
  2264.      Further patch to duplicate sections bug, and replacement of repeated values handling code.
  2265.  
  2266.      Revision 1.14  2000/11/29 11:26:03  grail
  2267.      Updates for task 22401 (no more reloadsig) and 22402 (Group and GroupMember doco)
  2268.  
  2269.      Revision 1.13  2000/11/28 12:41:42  grail
  2270.      Added test for being able to add sections with wierd names like section|version2
  2271.  
  2272.      Revision 1.11  2000/11/24 21:20:11  rbowen
  2273.      Resolved SourceForge bug #122445 - a parameter should be split from its value on the first = sign encountered, not on the last one. Added test suite to test this, and put test case in test.ini
  2274.  
  2275.      Revision 1.10  2000/11/24 20:40:58  rbowen
  2276.      Updated MANIFEST to have file list of new files in t/
  2277.      Updated IniFiles.pm to have mention of sourceforge addresses, rather than rcbowen.com addresses
  2278.      Regenerated README from IniFiles.pm
  2279.  
  2280.      Revision 1.9  2000/11/23 05:08:08  grail
  2281.      Fixed documentation for bug 122443 - Check that INI files can be created from scratch.
  2282.  
  2283.      Revision 1.1.1.1  2000/11/10 03:04:01  rbowen
  2284.      Initial checkin of the Config::IniFiles source to SourceForge
  2285.  
  2286.      Revision 1.8  2000/10/17 01:52:55  rbowen
  2287.      Patch from Jeremy. Fixed "defined" warnings.
  2288.  
  2289.      Revision 1.7  2000/09/21 11:19:17  rbowen
  2290.      Mostly documentation changes. I moved the change log into the POD rather
  2291.      than having it in a separate Changes file. This allows people to see the
  2292.      changes in the Readme before they download the module. Now I just
  2293.      need to make sure I remember to regenerate the Readme every time I do
  2294.      a commit.
  2295.  
  2296.  
  2297.      1.6 September 19, 2000 by JW, AS
  2298.      * Applied several patches submitted to me by Jeremy and Alex.
  2299.      * Changed version number to the CVS version number, so that I won't
  2300.      have to think about changing it ever again. Big version change
  2301.      should not be taken as a huge leap forward.
  2302.  
  2303.      0.12 September 13, 2000 by JW/WADG
  2304.      * Added documentation to clarify autovivification issues when 
  2305.      creating new sections
  2306.      * Fixed version number (Oops!)
  2307.  
  2308.      0.11 September 13, 2000 by JW/WADG
  2309.      * Applied patch to Group and GroupMembers functions to return empty
  2310.      list when no groups are present (submitted by John Bass, Sep 13)
  2311.  
  2312.      0.10 September 13, 2000 by JW/WADG
  2313.      * Fixed reference in POD to ReWriteFile. changes to RewriteConfig
  2314.      * Applied patch for failed open bug submitted by Mordechai T. Abzug Aug 18
  2315.      * Doc'd behavior of failed open
  2316.      * Removed planned SIG testing from test.pl as SIGs have been removed
  2317.      * Applied patch from Thibault Deflers to fix bug in parameter list
  2318.      when a parameter value is undef
  2319.  
  2320.      0.09
  2321.      Hey! Where's the change log for 0.09?
  2322.  
  2323.      0.08
  2324.      2000-07-30  Adrian Phillips  <adrianp@powertech.no>
  2325.  
  2326.      * test.pl: Fixed some tests which use $\, and made those that try
  2327.      to check a non existant val check against ! defined.
  2328.  
  2329.      * IniFiles.pm: hopefully fixed use of $\ when this is unset
  2330.      (problems found when running tests with -w).  Similar problem with
  2331.      $/ which can be undefined and trying to return a val which does
  2332.      not exist. Modified val docs section to indicate a undef return
  2333.      when this occurs.
  2334.  
  2335.      0.07
  2336.      Looks like we missed a change log for 0.07. Bummer.
  2337.  
  2338.      0.06 Sun Jun 25, 2000 by Daniel Winkelmann
  2339.      * Patch for uninitialized value bug in newval and setval
  2340.      
  2341.      0.05 Sun Jun 18, 2000 by RBOW
  2342.      * Added something to shut up -w on VERSIONS
  2343.      * Removed unused variables
  2344.  
  2345.      0.04 Thu Jun 15 - Fri Jun 16, 2000 by JW/WADG
  2346.      * Added support for -import option on ->new
  2347.      * Added support for tying a hash
  2348.      * Edited POD for grammer, clarity and updates
  2349.      * Updated test.pl file
  2350.      * Fixed bug in multiline/single line output
  2351.      * Fixed bug in default handling with tie interface
  2352.      * Added bugs to test.pl for regression
  2353.      * Fixed bug in {group} vs. {groups} property (first is valid)
  2354.      * Fixed return value for empty {sects} or {parms}{$sect} in
  2355.      Sections and Parameters methods
  2356.  
  2357.      0.03 Thu Jun 15, 2000 by RBOW
  2358.      * Modifications to permit 'use strict', and to get 'make test' working
  2359.      again.
  2360.  
  2361.      0.02 Tue Jun 13, 2000 by RBOW
  2362.      * Fixed bug reported by Bernie Cosell - Sections, Parameters, 
  2363.      and GroupMembers return undef if there are no sections,
  2364.      parameters, or group members. These functions now return
  2365.      () if the particular value is undefined.
  2366.      * Added some contributed documentation, from Alex Satrapa, explaining
  2367.      how the internal data structure works. 
  2368.      * Set up a project on SourceForge. (Not a change, but worth
  2369.      noting).
  2370.      * Added Groups method to return a list of section groups.
  2371.  
  2372.      0.01  Mon Jun 12, 2000 by RBOW
  2373.      Some general code cleanup, in preparation for changes to
  2374.      come. Put up Majordomo mailing list and sent invitation to
  2375.      various people to join it.
  2376.  
  2377. =cut
  2378. #[JW for editor]:mode=perl:tabSize=8:indentSize=2:noTabs=true:indentOnEnter=true:
  2379.  
  2380.