in reply to using a cmd-template

Hello goldenblue.

I am not sure if I fully understand what you are trying to accomplish. I am sure there is a better way to do this, but hopefully what I have listed below will help you. I placed the commands into a Perl module. I inserted tokens in the command hash that will be replaced based on the command input.

From a script you can include the Commands.pm module. The code below will need to be tweaked in order to parse the correct commands.

Commands.pm

#!/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;

Command_parser.pl

#/usr/bin/perl use strict; use warnings; use Commands 1.0; sub Get_Client(); my @clients; die "Error. Usage \'perl Command_parser.pl -Noclient | [-Command Clien +t1 Arguments -Command Client2 Arguments...] \'\n $!" unless $#ARGV > += 0; #################################################################### #################################################################### ## If a client was not provided at the command line, get one if (lc($ARGV[0]) eq "-noclient") { Get_Client(); } else { my @current_command; for(my $i = 0; $i <=$#ARGV; $i++) { if ( $ARGV[$i] =~ m/^\-/ && $i != 0) { # eval current command , then wipe it and keep processing my $command_value = eval_cmd(\@current_command); + print "Command is :$command_value\n"; ##system($command_value); @current_command = (); push @current_command , $ARGV[$i]; $current_command[0] =~ s/\-//g; } elsif ( $i == $#ARGV ) { # eval last command push @current_command , $ARGV[$i]; my $command_value = eval_cmd(\@current_command); print "Command is :$command_value\n"; ##system($command_value); } else { push @current_command , $ARGV[$i]; if ($i == 0) { $current_command[0] =~ s/\-//g; } } } } #################################################################### #################################################################### ## No client was provided at the command line, fill one in sub Get_Client() { my @current_command = ('backup','mars','file1.txt'); my $cmd_value = eval_cmd(\@current_command); # here magic happens! ##security_check($cmd); ##system("$cmd"); }

Input / Output

C:\monks\command_template>perl Command_parser.pl -backup client1 myfil +e1.txt -backup client2 myfile2.txt -list client3 Command is :backup client1 > myfile1.txt Command is :backup client2 > myfile2.txt Command is :list -le client3

Replies are listed 'Best First'.
Re^2: using a cmd-template
by goldenblue (Acolyte) on Dec 11, 2013 at 09:28 UTC

    Hi Vincent,

    many thanks to you, too for taking so much time without being sure what the question was!

    I like your 'token' aproach and working with listrefs, instead of using named vars.

    I feel like I know now why and where I got stuck, and your post helped me do that! Thank you!

    --
    the singularity will happen.