I’m working on a dispatching script. It takes a string with a command, does some cooking to it, and then parses it. But I can’t grab a hold into the referencing:
Use::strict;
Use:warnings;
my($contexto, $cmd, $target, $ultpos, @params);
my $do = "echo5 sample string that says stuff ";
$target = "";
$cmd = "";
$_ = "";
# I do some cumbersome string parsing to get the array with
# the exploded string and then call parsear(@command)
sub parsear {
my %operations = (
'echo' => \&echo,
'status' => \&status,
'echo5' => \&echo5,
);
my $op = $_[0];
if ($operations{$op}){
$operations{$op}->(@_);
print "it exists\n";
}
else{
print "incorrect command.\n";
}
}
sub status {
print "correct status.\n";
}
sub echo {
shift(@_);
print join(' ',@_) . "\n";
}
sub echo5 {
shift(@_);
print join(' ',@_) . "\n" x 5;
}
I don’t really know what the problem is. If the sub does not exist, it never says "incorrect command", and if I call for example "echo5 hello" it should print out:
hello
hello
hello
hello
hello
But it does nothing.
And when I call echo, it works as expected. What is the explanation?
Note: I’m on the latest version of Strawberry Perl
Then running:
results in:
I am not sure what "cumbersome string parsing" you are doing since you did not include it, but if you are parsing a string like
where the command is the first word, and the arguments are the rest, you can either split everything:
Or use a regex to cut the first word off:
You don’t even need the variables:
Finally, the
echo5subroutine is a bit more complicated than it needs to be. It could be written as: