use strict; # use is a keyword
use warnings;
# all these variables are not needed
sub parsear { # learn to indent correctly
my %operations = (
'echo' => \&echo,
'status' => \&status,
'echo5' => \&echo5,
);
my $op = shift; # take first element off @_
if ($operations{$op}) {
print "$op exists\n"; # make your status msg useful
$operations{$op}->(@_);
} else {
print "incorrect command: $op\n"; # and your error msg
}
}
sub status {
print "correct status.\n";
}
sub echo {
# shift(@_); # this is no longer needed, and now echo can be used as a
# normal subroutine as well as a dispatch target
print join(' ',@_) . "\n";
}
sub echo5 {
# shift(@_); # this is no longer needed
print +(join(' ',@_) . "\n") x 5; # parens needed since x binds tightly
}
затем выполняется:
parsear 'status';
parsear 'echo', 'hello';
parsear 'echo5', 'hello';
parsear 'an error';
приводит к:
status exists
correct status.
echo exists
hello
echo5 exists
hello
hello
hello
hello
hello
incorrect command: an error
Я не уверен, что cumbersome string parsing
вы делаете, поскольку вы его не включили, но есливы анализируете строку типа
my $do = "echo5 sample string that says stuff ";
, где команда является первым словом, а аргументы - остальными, вы можете либо разбить все:
parsear split /\s+/, $do;
или использовать регулярное выражение дляобрежьте первое слово:
my ($cmd, $arg) = $do =~ /^(\w+)\s*(.*)/;
parsear $cmd => $arg;
вам даже не нужны переменные:
parsear $do =~ /^(\w+)\s*(.*)/;
наконец, подпрограмма echo5
немного сложнее, чем нужнобыть.Это может быть записано как:
sub echo5 {
print "@_\n" x 5; # "@_" means join($", @_) and $" defaults to ' '
}