views:

239

answers:

3

I choose to use tie and find this:

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]}->{'='};
}

if I remove the last "$[0]->{$[1]}->{'='};", it does not work correctly. Why ?

I know a return value is required. But "$[0]->{$[1]};" cannot work correctly either, and $[0]->{$[1]}->{'='} is not the whole thing.


Old post:

I am write a package in Perl for parsing INI files. Just something based on Config::Tiny.

I want to keep the order of sections & keys, so I use extra array to store the order.

But when I use " $Config->{newsection} = { this => 'that' }; # Add a section ", I need to overload '=' so that "newsection" and "this" can be pushed in the array.

Is this possible to make "$Config->{newsection} = { this => 'that' };" work without influence other parts ?

Part of the code is:

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{']'}}) {
}}
+8  A: 

Special Symbols for Overload lists the behaviour of Perl overloading for '='.

The value for "=" is a reference to a function with three arguments, i.e., it looks like the other values in use overload. However, it does not overload the Perl assignment operator. This would go against Camel hair.

So you will probably need to rethink your approach.

UberAlex
So, that is why I didn't find anything on overload '='.
Galaxy
+5  A: 

This is not exactly JUST operator overloading, but if you absolutely need this functionality, you can try a perl tie: http://perldoc.perl.org/functions/tie.html

Goose Bumper
Well, this will be a chance for me to learn to use tie::
Galaxy
+5  A: 

Do you know about Config::IniFiles? You might consider that before you go off and reinvent it. With some proper subclassing, you can add ordering to it.

Also, I think you have the wrong interface. You're exposing the internal structure of your object and modifying it through magical assignments. Using methods would make your life much easier.

brian d foy
I saw Config::IniFiles, it seems a bit larger than Config::Tiny.Since I have to use it on other computers, I think writing a new single file pm is easier than copy several files to a new path.
Galaxy
Well, it's not hard to install modules with the cpan tool. You shouldn't be copying anyway. That's a different problem, though.
brian d foy