File: Ordered.pm

package info (click to toggle)
libconfig-ini-reader-ordered-perl 0.011-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 144 kB
  • ctags: 94
  • sloc: perl: 1,028; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 1,746 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use strict;

package Config::INI::Reader::Ordered;

use Config::INI::Reader;
use vars qw(@ISA $VERSION);
BEGIN { @ISA = qw(Config::INI::Reader) }

=head1 NAME

Config::INI::Reader::Ordered -- .ini-file parser that returns sections in order

=head1 VERSION

version 0.011

=cut

$VERSION = '0.011';

=head1 SYNOPSIS

If F<family.ini> contains:

  admin = rjbs

  [rjbs]
  awesome = yes
  height = 5' 10"

  [mj]
  awesome = totally
  height = 23"

Then when your program contains:

  my $array = Config::INI::Reader->read_file('family.ini');

C<$array> will contain:

  [
    [ '_'  => { admin => 'rjbs' } ],
    [
      rjbs => {
        awesome => 'yes',
        height  => q{5' 10"},
      }
    ],
    [ 
      mj   => {
        awesome => 'totally',
        height  => '23"',
      }
    ],
  ]

=head1 DESCRIPTION

Config::INI::Reader::Ordered is a subclass of L<Config::INI::Reader> which
preserves section order.  See L<Config::INI::Reader> for all documentation; the
only difference is as presented in the L</SYNOPSIS>.

=cut

=head1 METHODS

=head2 change_section

=head2 set_value

=head2 finalize

Overridden to preserve and present section order.

=cut

sub change_section {
  my ($self, $section) = @_;
  $self->SUPER::change_section($section);
  $self->{order} ||= [];
  push @{ $self->{order} }, $section
    unless grep { $_ eq $section } @{ $self->{order} };
}

sub set_value {
  my ($self, $name, $value) = @_;
  $self->SUPER::set_value($name, $value);
  unless ($self->{order}) {
    $self->{order} = [ $self->starting_section ];
  }
}

sub finalize {
  my ($self) = @_;
  my $data = [];
  for my $section (@{ $self->{order} || [] }) {
    push @$data, [ $section, $self->{data}{$section} ];
  }
  $self->{data} = $data;
}

1;