我选择使用领带,并发现:
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]}->{'='};
}如果删除最后一个"$->{$1}->{'='};",它将不能正常工作。为什么?
我知道返回值是必需的。但是“$->{$1}”也不能正常工作,$->{$1}->{'='}也不是全部。
旧员额:
我正在用Perl编写一个用于解析INI文件的包。只是一些基于Config::Tiny的东西。
我希望保持节和键的顺序,所以我使用额外的数组来存储订单。
但是当我使用“$Config->{newsection} = { this => 'that' }; # Add a section”时,我需要重载'=‘,以便可以在数组中推送"newsection“和"this”。
这有可能使"$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{']'}}) {
}}发布于 2009-09-27 16:05:19
这不仅仅是操作符重载,但是如果您绝对需要这个功能,您可以尝试perl tie:http://perldoc.perl.org/functions/tie.html。
发布于 2009-09-27 12:08:04
过载专用符号为'=‘列出了Perl重载的行为。
"=“的值是对带有三个参数的函数的引用,即它看起来类似于使用重载的其他值。但是,它不会重载Perl赋值操作符。这会对骆驼毛不利。
因此,您可能需要重新考虑您的方法。
发布于 2009-09-27 20:49:29
你知道Config::IniFiles的事吗?在你出发重新创造它之前,你可能会考虑这个问题。通过适当的子类,您可以向其添加排序。
还有,我觉得你的界面不对。您正在公开对象的内部结构,并通过神奇的任务对其进行修改。使用方法会使你的生活变得容易得多。
https://stackoverflow.com/questions/1483418
复制相似问题