The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Map::Tube - A very simple perl interface to the London Tube Map.

VERSION

Version 2.17

AWARD

Map::Tube has been granted the "Famous Software Award" by Download.FamousWhy.com on Tue 09 Nov 2010.

http://download.famouswhy.com/map_tube/

SYNOPSIS

      B --------  C
     /  \       /  \
    /    \     /    \
   /      \   /      \
  A ------  G ------- D
   \      /   \      /
    \    /     \    /
     \  /       \  /
      F -------- E
     /
    /
   /
  H
   \
    \
     \
      I

  which can be defined as below:

  { 'A' => ['B','F','G'],
    'B' => ['A','C','G'],
    'C' => ['B','D','G'],
    'D' => ['C','E','G'],
    'E' => ['D','F','G'],
    'F' => ['A','E','G','H'],
    'G' => ['A','B','C','D','E','F'],
    'H' => ['F','I'],
    'I' => ['H']
  }

DESCRIPTION

The module intends to provide you as much information as possible from London Tube Map through perl interface. The very first thing anyone would like to know from any map is to find the shortest route between two point. This is exactly what I am trying to solve at the moment. However I would be adding more interesting information very soon. This module covers some of the underground lines managed by Travel for London. It is far from complete and bound to have missing links and incorrect mapping. Please feel free to shout back to me, if you find any error/issue. While trying to find the shortest route, it takes into account the number of stops one has to go through to reach the destination. I do agree, at times, you wouldn't mind going through few extra stops, to avoid changing lines. I might add this behaviour in future. Please note Map::Tube doesn't try to explain Dijkstra's algorithm but to provide a perl interface to the London Tube Map. It covers Bakerloo, Central, Circle, District, DLR, Hammersmith & City, Jubilee, Metropolitan, Northern, Overground, Piccadilly, Victoria and Waterloo & City. Here is the link to the official London Tube Map: http://www.tfl.gov.uk/assets/downloads/standard-tube-map.pdf

CONSTRUCTOR

The constructor expects no parameters. This setup the default node definitions. By default the DEBUG is turned off.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

METHODS

get_shortest_route()

This method accepts FROM and TO node name. It is case insensitive. It returns back the node sequence from FROM to TO. It ignores multiple spaces in between node's name. It trims any space at the start and at the end of the node's name.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Find the shortest route from 'Bond Street' to 'Euston'.
  my @route = $map->get_shortest_route('Bond Street', 'Euston');

get_tube_lines()

This method accept node code and returns the line names of the given node code. This applies mainly to the default node definitions. For user defined node mappings it would return the node code itself.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Get tube lines for the node 'Wembley Park'.
  my @lines = $map->get_tube_lines('Wembley Park');

set_node_mappings()

This method accept the node defintion from user. It does some basic check i.e. the node data has to be reference to a HASH and each key has a value which is a reference to an ARRAY.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Define node mappings.
  my $mappings = { 'A' => ['B','F','G'],
                   'B' => ['A','C','G'],
                   'C' => ['B','D','G'],
                   'D' => ['C','E','G'],
                   'E' => ['D','F','G'],
                   'F' => ['A','E','G','H'],
                   'G' => ['A','B','C','D','E','F'],
                   'H' => ['F','I'],
                   'I' => ['H'],};

  # However user can override the node definition.
  $map->set_node_mappings($mappings);

  # Find the shortest route from 'C' to 'H'
  my @route = $map->get_shortest_route('C', 'H');

set_default_node_mappings()

This method set the default node mapping definitions.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Define node mappings.
  my $mappings = { 'A' => ['B','F','G'],
                   'B' => ['A','C','G'],
                   'C' => ['B','D','G'],
                   'D' => ['C','E','G'],
                   'E' => ['D','F','G'],
                   'F' => ['A','E','G','H'],
                   'G' => ['A','B','C','D','E','F'],
                   'H' => ['F','I'],
                   'I' => ['H'],};

  # However user can override the node mapping definitions.
  $map->set_node_mappings($mappings);

  # Find the shortest route from 'C' to 'H'
  my @route = $map->get_shortest_route('C', 'H');

  # Revert back to the default node mapping definitions.
  $map->set_default_node_mappings();

  # Find the shortest route from 'Bond Street' to 'Euston'.
  @route = $map->get_shortest_route('Bond Street', 'Euston');

get_node_mappings()

Returns all the nodes mapping defintions.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Get all the nodes mapping definitions.
  my $mappings = $map->get_node_mappings();

get_node_lines()

Returns all the tube line's defintions. For user defined node, it would return line definition provided by the user, if any, otherwise UNDEF.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Get the node lines definitions.
  my $lines = $map->get_node_lines();

set_node_lines()

Set the user defined node lines defintions.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Define node mappings.
  my $mappings = { 'A' => ['B'],
                   'B' => ['A','C'],
                   'C' => ['B','D','F'],
                   'D' => ['C','E'],
                   'E' => ['D','F'],
                   'F' => ['C','E'] };

  # Set the user node mapping definitions.
  $map->set_node_mappings($mappings);

  # Define user node lines definitions.
  my $lines = { 'Line1' => ['A','B','C','D','E','F'],
                'Line2' => ['C','F','E'] };

  # Set the user defined node line definitions.
  $map->set_node_lines($lines);

get_nodes()

Returns all the node defintions. For user defined nodes it would returned all the node codes.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Get all the node definitions.
  my $nodes = $map->get_nodes();

get_node_name()

This method takes a node code and returns its name. If the node belongs to user defined mapping then it simply returns the node code itself. It returns undef if it can't find the code.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintions with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Get node name for the given node code.
  my $name = $map->get_node_name('BST');

set_debug()

This method enables to turn the debug on or off.

  use strict; use warnings;
  use Map::Tube;

  # Setup the default node defintion with DEBUG turned OFF.
  my $map = Map::Tube->new();

  # Debug is turned on.
  $map->set_debug(1);

show_map_chart()

This method dumps the map chart used internally to find the shortest route to the STDOUT. This should only be called after get_shortest_route() to get some reasonable data. The map chart is generated by the internal method _process_node() which gets called by the method get_shortest_route(). This method takes no parameter. It has three columns by the title "N" - Node Code, "P" - Path to here and "L" - Length to reach "N" from "P".

  use strict; use warnings;
  use Map::Tube;

  # This setup the default nodes ready to be use.
  my $map = Map::Tube->new();

  # Define node mappings.
  my $mappings = { 'A' => ['B','F','G'],
                   'B' => ['A','C','G'],
                   'C' => ['B','D','G'],
                   'D' => ['C','E','G'],
                   'E' => ['D','F','G'],
                   'F' => ['A','E','G','H'],
                   'G' => ['A','B','C','D','E','F'],
                   'H' => ['F','I'],
                   'I' => ['H'],};

  # However user can override the default node mapping definitions.
  $map->set_node_mappings($mappings);

  # Find the shortest route from 'C' to 'H'
  my @route = $map->get_shortest_route('C', 'H');

  # The map chart will have meaningfull data only after you
  # have called method get_shortest_route().
  $map->show_map_chart();

  # You should expect the following output: 
  #  N  -  P  -  L
  # ---------------
  #  A  -  B  -  2
  #  B  -  C  -  1
  #  C  -  C  -  0
  #  D  -  C  -  1
  #  E  -  D  -  2
  #  F  -  G  -  2
  #  G  -  C  -  1
  #  H  -  F  -  3
  #  I  -  H  -  4
  # ---------------

AUTHOR

Mohammad S Anwar, <mohammad.anwar@yahoo.com>

ACKNOWLEDGEMENTS

Peter Makholm (http://search.cpan.org/~pmakholm/) for valuable advice.

BUGS

Please report any bugs or feature requests to bug-map-tube@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Map-Tube. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Map::Tube

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright 2010 Mohammad S Anwar.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

DISCLAIMER

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.