#!/usr/bin/perl use strict; use warnings; package Commands; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=1.0; our @ISA = qw(Exporter); our @EXPORT = qw(%cmd_tmpl eval_cmd); our @EXPORT_OK = qw(); our sub eval_cmd ($); #################################################################### #################################################################### ## Command Hash template our %cmd_tmpl = ( 'backup' => 'token token > token', 'list' => 'token -le token' ); ## Evaluate command , accepts Command , Client, and Arguments our sub eval_cmd ($) { my $array_ref = shift; my $cmd = @{$array_ref}[0]; my $client = @{$array_ref}[1]; my @args; if ( defined @{$array_ref}[2] ) { @args = split(/ /,@{$array_ref}[2]); } die "No such command: $cmd!" unless exists $cmd_tmpl{lc($cmd)}; ## convert command to lc internally my $cmd_value = $cmd_tmpl{$cmd}; ## update command , replaces first 'token' encountered. $cmd_value =~ s/token/$cmd/; ## update client , replaces first 'token' encountered. $cmd_value =~ s/token/$client/; ## update arguments , replaces first 'token' encountered. for(my $i = 0; $i <=$#args; $i++) { $cmd_value =~ s/token/$args[$i]/; } if ($cmd_value =~ m/token/) { die "Not enough aruments on [@{$array_ref}]"; } return $cmd_value; } #################################################################### #################################################################### 1;