HashToMenu
NAME
Menu::Generate - Generate a Simple::Menu object from a hash data structure
SYNOPSIS
Simple usage:
use Menu::HashToMenu;
my %hash = 'Option A' => 'Value A',
'Option B' => 'Value B',
'Option C' =>
{submenu1 =>
{ subsubmenu => 1},
submenu2 => 'hello' };
my $menu = HashToMenu.new(%hash);
$menu.execute
Advanced usage with value actions and value processing:
use Menu::HashToMenu;
my %hash =
'Option A' => 'Value A',
'Option B' => 'Value B',
'Option C' =>
{submenu1 =>
{ subsubmenu => 'hello'},
'submenu option' => 'hello' };
my &processor = sub ($value_from_hash) { flip $value_from_hash };
my &action = sub ($processed_value) { say $processed_value };
my $menu = HashToMenu.new(%hash, &action, $processor);
$menu.execute
DESCRIPTION
The class's constructor accepts a require hash object and two optional arguments for processing option values associated with an option. The class immediately returns a Menu::Simple object which can be executed directly.
The class can automatically generate nested submenus and the actions to be performed when an option is selected.
METHODS
multi method new(Hash:D $hash)
multi method new(Hash:D $hash, &value-action)
multi method new(Hash:D $hash, :&value-action, :&value-processor)
%hash
A hash representing the menu. Keys are used as the $display-string
shown on the menu. Submenus are created by assigning a hash to a key.
Hash values that are not a hash are used as the $option-value for
the option.
&action
This is the callable that will be performed when an option is selected.
&processor
This is the callable that can be used to process the value associated with a menu
before getting passed to the &action callable. If no &processor is supplied,
the value from the hash is passed directly to the option's &action callable.