in reply to Re: Read INI file
in thread Read INI file

Hi Thanks for the reply, My .ini file is
[Test_suit_name] Testcase1.txt
Basically, this ini file have many test suits and under that many test cases I want to select the test cases based on test suits. Hope this clears.

Replies are listed 'Best First'.
Re^3: Read INI file
by SimonPratt (Friar) on Apr 05, 2016 at 14:40 UTC
    use strict; use warnings; open my $fh, '<', 'an.ini' or die "Failed to open an.ini for reading: +$! <$^E>\n"; my (%settings, $head); while (chomp(my $line = <$fh>)) { if ($line =~ /^\s*\[([^\[\]]+)\]\s*$/) { $head = $1; } elsif ($line !~ /^\s*$/) { push @{$settings{$head}}, $line; } } close $fh; use Data::Dumper; print Dumper \%settings;
Re^3: Read INI file
by FreeBeerReekingMonk (Deacon) on Apr 06, 2016 at 10:31 UTC
    Building on SimonPratt's work:

    #!/usr/bin/perl use strict; use warnings; use Getopt::Long; my $VERBOSE; my @TESTSUITES; my %TESTCASES_hash; my @TESTCASES; GetOptions ( "testsuite=s" => \@TESTSUITES, "verbose" => \$VERBOSE ); # Often it is useful to allow comma-separated lists of values as well +as multiple occurrences of the options. @TESTSUITES = split(/\s*,\s*/,join(',', @TESTSUITES)); open my $fh, '<', 'an.ini' or die "Failed to open an.ini for reading: +$! <$^E>\n"; my (%settings, $head); while (chomp(my $line = <$fh>)) { if ($line =~ /^\s*\[([^\[\]]+)\]\s*$/) { $head = $1; } elsif ($line !~ /^\s*$/) { push @{$settings{$head}}, $line; } } close $fh; for my $suite (@TESTSUITES){ for my $test (@{$settings{$suite}}){ $TESTCASES_hash{$test}++; } } @TESTCASES = sort keys %TESTCASES_hash; use Data::Dumper; print Dumper \%settings; print Dumper \@TESTCASES; for my $testcase (@TESTCASES){ # now iterate through your testcases }

    Which would work like:

    ./an.pl -t Test_suit_name ./an.pl -t Test_suit_name -t AnotherTest_suit_name ./an.pl -t Test_suit_name,AnotherTest_suit_name

    latter two are the same, multiple testsuites are merged to produce a bigger list of testcases.

    Hope this extra push in the back helps you...

Re^3: Read INI file
by bhushanQA (Sexton) on Apr 05, 2016 at 12:43 UTC
    Hi, Any clue on this ?
      If you changed this to:
      [Test_suit_name] Testcase1.txt = 1 Testcase2.txt = 0 TestCase3.txt = 1
      or something similar, you will have something that the standard .INI file modules will understand. And you can add the feature of "turning a test case off" without removing a line from the [Test_suit_name] section. I use this format for one program's default .INI file where almost all options are turned off (0), by default. The user can see what can be enabled without having to read a separate doc and add a line to the .ini file. Just change a 0 to a 1.

      Update: it is also possible to have something like:  test_suite_names = test1 test2 test8 In that case you get a hash key of test_suite_names and the text to the right of the "=" wind up being a string that you can parse. If possible, I would "go with the flow" and use a syntax that the standard modules can deal with.