Can I overload Perl =? (And the problem when using Tie)

I want to use a 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 doesn't work properly. Why?

I know the return value is required. But "$ [0] โ†’ {$ [1]}; also cannot work correctly, and $ [0] โ†’ {$ [1]} โ†’ {'='} is not all.


Old post:

I am writing a package in Perl to parse INI files. Just something based on Config::Tiny

.

I want to preserve the order of sections and keys, so I use an additional array to store the order.

But when I use " $Config->{newsection} = { this => 'that' }; # Add a section

", I need to overload the ' =

' so that "newsection" and "this" are pressed in the array.

Is it possible that " $Config->{newsection} = { this => 'that' };

" works without affecting other parts?

Part of the code:

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

      

+2


source to share


3 answers


This isn't just an overload of the JUST operator, but if you absolutely need this functionality, you can try perl tie: http://perldoc.perl.org/functions/tie.html



+5


source


Special Characters for Overloading lists the overloading behavior of Perl for '='.

The value for "=" is a function reference with three arguments, so it looks like other values โ€‹โ€‹used in overloading. However, it does not overload Perl's assignment operator. This will go against the camel's hair.



As such, you may have to rethink your approach.

+8


source


Do you know about Config :: IniFiles ? You might want to think about this before you go out and reinvent it. With some correct subclass, you can add ordering to it.

Also, I think you have the wrong interface. You reveal the inner structure of your object and change it through magical tasks. Using techniques will make your life easier.

+5


source







All Articles