私はネクタイを使用することを選択し、これを見つけます:
package Galaxy::IO::INI;
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {']' => []}; # ini section can never be ']'
tie %{$self},'INIHash';
return bless $self, $class;
}
package INIHash;
use Carp;
require Tie::Hash;
@INIHash::ISA = qw(Tie::StdHash);
sub STORE {
#$_[0]->{$_[1]} = $_[2];
push @{$_[0]->{']'}},$_[1] unless exists $_[0]->{$_[1]};
for (keys %{$_[2]}) {
next if $_ eq '=';
push @{$_[0]->{$_[1]}->{'='}},$_ unless exists $_[0]->{$_[1]}->{$_};
$_[0]->{$_[1]}->{$_}=$_[2]->{$_};
}
$_[0]->{$_[1]}->{'='};
}
最後の「$ [0]->{$ [1]}->{'='};」を削除すると、正しく動作しません。なんで ?
戻り値が必要であることはわかっています。しかし、「$ [0]->{$ [1]};」$ [0]->{$ [1]}->{'='} がすべてではありません。
古い投稿:
INI ファイルを解析するためのパッケージを Perl で作成しています。に基づいたものですConfig::Tiny
。
セクションとキーの順序を保持したいので、余分な配列を使用して順序を格納します。
しかし、" " を使用するときは、"newsection" と "this" を配列にプッシュできるように、$Config->{newsection} = { this => 'that' }; # Add a section
' ' をオーバーロードする必要があります。=
$Config->{newsection} = { this => 'that' };
これにより、他のパーツに影響を与えずに " " を動作させることは可能ですか?
コードの一部は次のとおりです。
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {']' => []}; # ini section can never be ']'
return bless $self, $class;
}
sub read_string {
if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) {
$self->{$ns = $1} ||= {'=' => []}; # ini key can never be '='
push @{$$self{']'}},$ns;
next;
}
if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
push @{$$self{$ns}{'='}},$1 unless defined $$self{$ns}{$1};
$self->{$ns}->{$1} = $2;
next;
}
}
sub write_string {
my $self = shift;
my $contents = '';
foreach my $section (@{$$self{']'}}) {
}}