in reply to Parameters not in order by using Config::Tiny

I was not aware of Tiny::Config::Ordered until today. However, the Hash::Ordered module v0.009 is quite nice IMO.

The following diff is all that's needed to preserve order transparently in Tiny::Config.

$ diff -u Tiny.pm.orig Tiny.pm --- Tiny.pm.orig 2015-07-06 12:51:54.000000000 -0400 +++ Tiny.pm 2015-07-06 13:02:22.000000000 -0400 @@ -3,6 +3,7 @@ # If you thought Config::Simple was small... use strict; +use Hash::Ordered; # Warning: There is another version line, in t/02.main.t. @@ -39,7 +40,8 @@ # Create an object from a string sub read_string { my $class = ref $_[0] ? ref shift : shift; - my $self = bless {}, $class; + tie my %obj, 'Hash::Ordered'; + my $self = bless \%obj, $class; return undef unless defined $_[0]; # Parse the file @@ -59,7 +61,10 @@ # Create the sub-hash if it doesn't exist. # Without this sections without keys will not # appear at all in the completed struct. - $self->{$ns = $1} ||= {}; + $self->{$ns = $1} ||= do{ + tie my %obj, 'Hash::Ordered'; + \%obj; + }; next; }

The output order matches the ini file (sections and elements).

$ perl test.pl [insert] SQL1 = insert into table <table1> SQL2 = insert into table <table2> SQL3 = insert into table <table3> SQL4 = insert into table <table4> [Truncate] SQL1 = truncate table <tablename1> SQL2 = truncate table <tablename2> [update] SQL1 = update into <table1> SQL2 = update into <table2>

I sent the link to the first post to both authors.

Kind regards, Mario

Replies are listed 'Best First'.
Re^2: Parameters not in order by using Config::Tiny
by bhushanQA (Sexton) on Jul 07, 2015 at 10:41 UTC
    Thanks all for the help.. the best solution found by using 'sort' keyword.. which makes my job easy...and its working perfectly..however I have gone through the other solutions as well.. Appreciate.. right now I don't want to change my logic which is working and printing all section and parameter in order.  use Config::Tiny::Ordered; this is quite helpful.