Source

Kephra / lib / Kephra / CommandList.pm

Full commit
use strict;
use warnings;

package Kephra::CommandList;

my %list;
my %namespace;
my %object_alias;

sub register_object_switch_var {
	return unless $_[0] and ref $_[0] eq ref {};
	my %alias = %{$_[0]};
	for my $var ( keys %alias ){
		Kephra::Log::error
			("register_object_switch_var: $var already registered as ".$object_alias{$var})
				if exists $object_alias{$var};
		my $switch = $alias{$var};
		$switch = (caller)[0] . '::' . $switch if index($switch, '::') == -1;
		$object_alias{$var} = $switch;
	}
}
sub register_cmd_namespace {
	Kephra::Log::error
		("register_cmd_namespace: called without value from ". (caller)[0] )
			unless $_[0];
	$namespace{ (caller)[0] } = $_[0];
}
sub register_cmd {
	my $cmd = shift;
	$cmd = [$cmd] unless ref $cmd eq ref [];
	for my $cmd (@$cmd) {
		die "$cmd already registered to do ". cmd_property($cmd, 'function') .
		                             " by " . cmd_property($cmd, 'source')
			if cmd_exists( $cmd );
		#$list{$cmd}{'function'} = 
	}
}

sub init {
	%list = %{ Kephra::Config::Default::commandlist() };
	for (values %list){
		$_->{'callref'} = eval 'sub {'. $_->{'function'}.'}';
	}
}

sub cmd_exists          { 1 if defined $_[0]     and exists $list{ $_[0] }          }
sub cmd_property_exists { 1 if cmd_exists($_[0]) and exists $list{ $_[0] }{ $_[1] } }
sub cmd_add {}

sub cmd_all_properties { $list{$_[0]}                if cmd_exists($_[0]) }
sub cmd_property       { $list{$_[0]}{$_[1]}         if cmd_property_exists(@_) }
sub cmd_set_property   { $list{$_[0]}{$_[1]} = $_[2] if cmd_property_exists(@_) }


1;