#! /usr/bin/perl -w

use strict;

use Geo::Postcodes::DK 0.21;

################################################################################
#                                                                              #
#                                selection_dk                                  #
#                                ------------                                  #
#               Arne Sommer - perl@bbop.org - 04. September 2006               #
#                                                                              #
################################################################################

unless (@ARGV == 2)
{
  print "Usage: selection_dk <method> <string>\n";
  print "Legal methods: ", join(" ", Geo::Postcodes::DK::get_methods()), ".\n";
  exit;
}

my $method = shift(@ARGV);
my $string = shift(@ARGV);

my @methods;

if (Geo::Postcodes::DK::is_method($method)) # Legal method
{
  @methods = Geo::Postcodes::DK::get_methods();
    # Get all the methods supported by this module.

  foreach my $m (@methods)
  {
    printf("%-14s", ucfirst($m)); # Print headers for each column
  }
  print "\n";

  Geo::Postcodes::DK->selection_loop(\&print_it, $method, $string);
    # Call each postcode object on to the 'loop' procedure.

  print "\n";
}
else # illegal method.
{
  print "Not a legal method '$method'.\n";
}

sub print_it
{
  my $object = shift;
  foreach my $m (@methods)                # Iterate over the methods for each object
  {
    printf("%-14s", $object->$m() || ""); # Print the column data, or nothing if empty
  }
  print "\n";
}
