📄 inifiles.pm
字号:
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.
To un-set the EOT text, use DeleteParameterEOT ($section, $parameter).
=cut
sub SetParameterEOT
{
my $self = shift;
my $section = shift;
my $parameter = shift;
my $EOT = shift;
defined($section) || return undef;
defined($parameter) || return undef;
defined($EOT) || return undef;
if (not exists $self->{EOT}{$section}) {
$self->{EOT}{$section} = {};
}
$self->{EOT}{$section}{$parameter} = $EOT;
}
=head2 DeleteParameterEOT ($section, $parameter)
Removes the EOT marker for the given section and parameter.
When writing a configuration file, if no EOT marker is defined
then "EOT" is used.
=cut
sub DeleteParameterEOT
{
my $self = shift;
my $section = shift;
my $parameter = shift;
defined($section) || return undef;
defined($parameter) || return undef;
delete $self->{EOT}{$section}{$parameter};
}
=head2 DeleteSection ( $section_name )
Completely removes the entire section from the configuration.
=cut
sub DeleteSection {
my $self = shift;
my( $section_name ) = @_;
# This is done, the fast way, change if delval changes!!
delete $self->{v}{$section_name};
delete $self->{sCMT}{$section_name};
delete $self->{pCMT}{$section_name};
delete $self->{EOT}{$section_name};
delete $self->{parms}{$section_name};
@{$self->{sects}} = grep !/\Q$section_name\E$/, @{$self->{sects}};
if( $section_name =~ /(\S+)\s+\S+/ ) {
my $group = $1;
if( defined($self->{group}{$group}) ) {
@{$self->{group}{$group}} = grep !/\Q$section_name\E/, @{$self->{group}{$group}};
} # end if
} # end if
return 1;
} # end DeleteSection
=head2 Delete
Deletes the entire configuration file in memory.
=cut
sub Delete {
my $self = shift;
# Again, done the fast way, if the data structure changes, change this!
$self->{sects} = [];
$self->{parms} = {};
$self->{group} = {};
$self->{v} = {};
$self->{sCMT} = {};
$self->{pCMT} = {};
$self->{EOT} = {};
return 1;
} # end Delete
=head1 USAGE -- Tied Hash
=head2 tie $ini, 'IniFiles', (-file=>$filename, [-option=>value ...] )
Using C<tie>, you can tie a hash to a B<IniFiles> object. This creates a new
object which you can access through your hash, so you use this instead of the
B<new> method. This actually creates a hash of hashes to access the values in
the INI file. The options you provide through C<tie> are the same as given for
the B<new> method, above.
Here's an example:
use IniFiles;
my %ini
tie %ini, 'IniFiles', ( -file => "/path/configfile.ini" );
print "We have $ini{Section}{Parameter}." if $ini{Section}{Parameter};
Accessing and using the hash works just like accessing a regular hash and
many of the object methods are made available through the hash interface.
For those methods that do not coincide with the hash paradigm, you can use
the Perl C<tied> function to get at the underlying object tied to the hash
and call methods on that object. For example, to write the hash out to a new
ini file, you would do something like this:
tied( %ini )->WriteConfig( "/newpath/newconfig.ini" ) ||
die "Could not write settings to new file.";
=head2 $val = $ini{$section}{$parameter}
Returns the value of $parameter in $section.
Because of limitations in Perl's tie implementation,
multiline values accessed through a hash will I<always> be returned
as a single value with each line joined by the default line
separator ($\). To break them apart you can simple do this:
@lines = split( "$\", $ini{section}{multi_line_parameter} );
=head2 $ini{$section}{$parameter} = $value;
Sets the value of C<$parameter> in C<$section> to C<$value>.
To set a multiline or multiv-alue parameter just assign an
array reference to the hash entry, like this:
$ini{$section}{$parameter} = [$value1, $value2, ...];
If the parameter did not exist in the original file, it will
be created. However, Perl does not seem to extend autovivification
to tied hashes. That means that if you try to say
$ini{new_section}{new_paramters} = $val;
and the section 'new_section' does not exist, then Perl won't
properly create it. In order to work around this you will need
to create a hash reference in that section and then assign the
parameter value. Something like this should do nicely:
$ini{new_section} = {};
$ini{new_section}{new_paramters} = $val;
=head2 %hash = %{$ini{$section}}
Using the tie interface, you can copy whole sections of the
ini file into another hash. Note that this makes a copy of
the entire section. The new hash in no longer tied to the
ini file, In particular, this means -default and -nocase
settings will not apply to C<%hash>.
=head2 $ini{$section} = {}; %{$ini{$section}} = %parameters;
Through the hash interface, you have the ability to replace
the entire section with a new set of parameters. This call
will fail, however, if the argument passed in NOT a hash
reference. You must use both lines, as shown above so that
Perl recognizes the section as a hash reference context
before COPYing over the values from your C<%parameters> hash.
=head2 delete $ini{$section}{$parameter}
When tied to a hash, you can use the Perl C<delete> function
to completely remove a parameter from a section.
=head2 delete $ini{$section}
The tied interface also allows you to delete an entire
section from the ini file using the Perl C<delete> function.
=head2 %ini = ();
If you really want to delete B<all> the items in the ini file, this
will do it. Of course, the changes won't be written to the actual
file unless you call B<RewriteConfig> on the object tied to the hash.
=head2 Parameter names
=over 4
=item my @keys = keys %{$ini{$section}}
=item while (($k, $v) = each %{$ini{$section}}) {...}
=item if( exists %{$ini{$section}}, $parameter ) {...}
=back
When tied to a hash, you use the Perl C<keys> and C<each>
functions to iteratively list the parameters (C<keys>) or
parameters and their values (C<each>) in a given section.
You can also use the Perl C<exists> function to see if a
parameter is defined in a given section.
Note that none of these will return parameter names that
are part if the default section (if set), although accessing
an unknown parameter in the specified section will return a
value from the default section if there is one.
=head2 Section names
=over 4
=item foreach( keys %ini ) {...}
=item while (($k, $v) = each %ini) {...}
=item if( exists %ini, $section ) {...}
=back
When tied to a hash, you use the Perl C<keys> and C<each>
functions to iteratively list the sections in the ini file.
You can also use the Perl C<exists> function to see if a
section is defined in the file.
=cut
############################################################
#
# TIEHASH Methods
#
# Description:
# These methods allow you to tie a hash to the
# IniFiles object. Note that, when tied, the
# user wants to look at thinks like $ini{sec}{parm}, but the
# TIEHASH only provides one level of hash interace, so the
# root object gets asked for a $ini{sec}, which this
# implements. To further tie the {parm} hash, the internal
# class IniFiles::_section, is provided, below.
#
############################################################
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000May09 Created method JW
# ----------------------------------------------------------
sub TIEHASH {
my $class = shift;
my %parms = @_;
# Get a new object
my $self = $class->new( %parms );
return $self;
} # end TIEHASH
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000May09 Created method JW
# ----------------------------------------------------------
sub FETCH {
my $self = shift;
my( $key ) = @_;
$key = lc($key) if( $self->{nocase} );
return $self->{v}{$key};
} # end FETCH
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000Jun14 Fixed bug where wrong ref was saved JW
# 2000Oct09 Fixed possible but in %parms with defaults JW
# 2001Apr04 Fixed -nocase problem in storing JW
# ----------------------------------------------------------
sub STORE {
my $self = shift;
my( $key, $ref ) = @_;
return undef unless ref($ref) eq 'HASH';
$key = lc($key) if( $self->{nocase} );
# Create a new hash and tie it to a _sections object with the ref's data
$self->{v}{$key} = {};
# Store the section name in the list
push(@{$self->{sects}}, $key) unless (grep /^\Q$key\E$/, @{$self->{sects}});
my %parms = %{$self->{startup_settings}};
$self->{parms}{$key} = [];
$parms{-parms} = $self->{parms}{$key};
$parms{-_current_value} = $ref;
delete $parms{default};
$parms{-default} = $self->{v}{$parms{-default}} if defined $parms{-default} && defined $self->{v}{$parms{-default}};
tie %{$self->{v}{$key}}, 'IniFiles::_section', %parms;
} # end STORE
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000May09 Created method JW
# 2000Dec17 Now removes comments, groups and EOTs too JW
# 2001Arp04 Fixed -nocase problem JW
# ----------------------------------------------------------
sub DELETE {
my $self = shift;
my( $key ) = @_;
$key = lc($key) if( $self->{nocase} );
delete $self->{sCMT}{$key};
delete $self->{pCMT}{$key};
delete $self->{EOT}{$key};
delete $self->{parms}{$key};
if( $key =~ /(\S+)\s+\S+/ ) {
my $group = $1;
if( defined($self->{group}{$group}) ) {
@{$self->{group}{$group}} = grep !/\Q$key\E/, @{$self->{group}{$group}};
} # end if
} # end if
@{$self->{sects}} = grep !/^\Q$key\E$/, @{$self->{sects}};
return delete( $self->{v}{$key} );
} # end DELETE
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000May09 Created method JW
# ----------------------------------------------------------
sub CLEAR {
my $self = shift;
foreach (keys %{$self->{v}}) {
$self->DELETE( $_ );
} # end foreach
} # end CLEAR
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000May09 Created method JW
# ----------------------------------------------------------
sub FIRSTKEY {
my $self = shift;
my $a = keys %{$self->{v}};
return each %{$self->{v}};
} # end FIRSTKEY
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000May09 Created method JW
# ----------------------------------------------------------
sub NEXTKEY {
my $self = shift;
my( $last ) = @_;
return each %{$self->{v}};
} # end NEXTKEY
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000May09 Created method JW
# 2001Apr04 Fixed -nocase bug and false true bug JW
# ----------------------------------------------------------
sub EXISTS {
my $self = shift;
my( $key ) = @_;
$key = lc($key) if( $self->{nocase} );
return exists $self->{v}{$key};
} # end EXISTS
# ----------------------------------------------------------
# DESTROY is used by TIEHASH and the Perl garbage collector,
# ----------------------------------------------------------
# Date Modification Author
# ----------------------------------------------------------
# 2000May09 Created method JW
# ----------------------------------------------------------
sub DESTROY {
# my $self = shift;
} # end if
############################################################
#
# INTERNAL PACKAGE: IniFiles::_section
#
# Description:
# This package is used to provide a single-level TIEHASH
# interface to the sections in the IniFile. When tied, the
# user wants to look at thinks like $ini{sec}{parm}, but the
# TIEHASH only provides one level of hash interace, so the
# root object gets asked for a $ini{sec} and must return a
# has reference that accurately covers the '{parm}' part.
#
# This package is only used when tied and is inter-woven
# between the sections and their parameters when the TIEHASH
# method is called by Perl. It's a very simple implementation
# of a tied hash object with support for the IniFiles
# -nocase and -default options.
#
############################################################
# Date Modification Author
# ----------------------------------------------------------
# 2000.May.09 Created to excapsulate TIEHASH interface JW
############################################################
package IniFiles::_section;
use strict;
use Carp;
use vars qw( $VERSION );
$IniFiles::_section::VERSION = 2.16;
# ----------------------------------------------------------
# Sub: IniFiles::_section::TIEHASH
#
# Args: $class, %parms
# $class The class that this is being tied to.
# %parms Contains named parameters passed from the
# constructor plus thes parameters
# -_current_value holds the values to be inserted in the hash.
# -default should be a hash ref.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -