Perl-如何创建用户可以在控制台中输入的命令?
我刚刚开始学习Perl,我非常喜欢它。我正在编写一些基本函数,但我真正希望能够使用控制台命令智能地使用这些函数。例如,假设我有一个函数,将两个数字相加。我希望能够在控制台中键入“add2,4”并读取第一个单词,然后将这两个数字作为参数传递给“add”函数。本质上,我在使用Perl^^创建一些基本脚本方面寻求帮助 关于如何在VB中实现这一点,我有一些模糊的想法,但是Perl,我不知道从哪里开始,或者什么函数对我有用。是否有类似VB.net的“拆分”函数的功能,可以将标量的内容分解为数组?例如,有没有一种简单的方法可以一次分析一个标量中的一个单词,或者迭代一个标量直到找到分隔符 我希望你能帮忙,任何建议都将不胜感激!请记住,我不是专家,我在几个星期前就开始学习Perl,而我只做了半年VB.net 谢谢大家!Perl-如何创建用户可以在控制台中输入的命令?,perl,scripting,Perl,Scripting,我刚刚开始学习Perl,我非常喜欢它。我正在编写一些基本函数,但我真正希望能够使用控制台命令智能地使用这些函数。例如,假设我有一个函数,将两个数字相加。我希望能够在控制台中键入“add2,4”并读取第一个单词,然后将这两个数字作为参数传递给“add”函数。本质上,我在使用Perl^^创建一些基本脚本方面寻求帮助 关于如何在VB中实现这一点,我有一些模糊的想法,但是Perl,我不知道从哪里开始,或者什么函数对我有用。是否有类似VB.net的“拆分”函数的功能,可以将标量的内容分解为数组?例如,有没
编辑:如果您不确定要建议什么,并且您知道任何可能有帮助的简单/直观的资源,也将不胜感激。您希望解析命令行参数。一个
空格
用作分隔符,因此只需执行一个/add.pl 2 3
类似的操作:
$num1=$ARGV[0];
$num2=$ARGV[1];
print $num1 + $num2;
将打印要分析命令行参数的
5
。一个空格
用作分隔符,因此只需执行一个/add.pl 2 3
类似的操作:
$num1=$ARGV[0];
$num2=$ARGV[1];
print $num1 + $num2;
将打印
5
这里是一个简单脚本语言的简短实现
每条语句只有一行,并且具有以下结构:
Statement = [<Var> =] <Command> [<Arg> ...]
# This is a regular grammar, so we don't need a complicated parser.
学习Perl时,严格的
和警告
是必不可少的,否则可能会有太多奇怪的东西。use 5.010
是最低版本,它还定义了say
内置(类似于print
,但附加了一个换行符)
现在我们声明两个全局变量:%env
hash(表或dict)将变量名与其值相关联<代码>%functions保存我们的内置函数。这些值是匿名函数
my %env;
my %functions = (
add => sub { $_[0] + $_[1] },
mul => sub { $_[0] * $_[1] },
say => sub { say $_[0] },
bye => sub { exit 0 },
);
现在是read-eval循环(默认情况下不打印)。readline操作符
将从指定为第一个命令行参数的文件中读取,如果没有提供文件名,则从STDIN中读取
while (<>) {
next if /^\s*\#/; # jump comment lines
# parse the line. We get a destination $var, a $command, and any number of @args
my ($var, $command, @args) = parse($_);
# Execute the anonymous sub specified by $command with the @args
my $value = $functions{ $command }->(@args);
# Store the return value if a destination $var was specified
$env{ $var } = $value if defined $var;
}
Perl数组可以像链表一样处理:shift
删除并返回第一个元素(pop
对最后一个元素执行相同的操作)<代码>推送将元素添加到末尾,将取消移动
添加到开头
Out little编程语言可以执行以下简单程序:
#!my_little_language
$a = mul 2 20
$b = add 0 2
$answer = add $a $b
say $answer
bye
如果(1)我们的perl脚本保存在my_little_language
中,设置为可执行,并且位于系统路径中,并且(2)上述文件保存在中,表示_life.mll
,并且也设置为可执行,那么
$ ./meaning_of_life
应该能够运行它
输出显然是
42
。注意,我们的语言还没有字符串操作或简单的变量赋值。另外,能够直接使用其他函数的返回值调用函数也很好。这需要某种排列或优先机制。此外,该语言还需要更好的批处理错误报告功能(它已经支持该功能)。下面是一个简单脚本语言的简短实现
每条语句只有一行,并且具有以下结构:
Statement = [<Var> =] <Command> [<Arg> ...]
# This is a regular grammar, so we don't need a complicated parser.
学习Perl时,严格的
和警告
是必不可少的,否则可能会有太多奇怪的东西。use 5.010
是最低版本,它还定义了say
内置(类似于print
,但附加了一个换行符)
现在我们声明两个全局变量:%env
hash(表或dict)将变量名与其值相关联<代码>%functions保存我们的内置函数。这些值是匿名函数
my %env;
my %functions = (
add => sub { $_[0] + $_[1] },
mul => sub { $_[0] * $_[1] },
say => sub { say $_[0] },
bye => sub { exit 0 },
);
现在是read-eval循环(默认情况下不打印)。readline操作符
将从指定为第一个命令行参数的文件中读取,如果没有提供文件名,则从STDIN中读取
while (<>) {
next if /^\s*\#/; # jump comment lines
# parse the line. We get a destination $var, a $command, and any number of @args
my ($var, $command, @args) = parse($_);
# Execute the anonymous sub specified by $command with the @args
my $value = $functions{ $command }->(@args);
# Store the return value if a destination $var was specified
$env{ $var } = $value if defined $var;
}
Perl数组可以像链表一样处理:shift
删除并返回第一个元素(pop
对最后一个元素执行相同的操作)<代码>推送将元素添加到末尾,将取消移动
添加到开头
Out little编程语言可以执行以下简单程序:
#!my_little_language
$a = mul 2 20
$b = add 0 2
$answer = add $a $b
say $answer
bye
如果(1)我们的perl脚本保存在my_little_language
中,设置为可执行,并且位于系统路径中,并且(2)上述文件保存在中,表示_life.mll
,并且也设置为可执行,那么
$ ./meaning_of_life
应该能够运行它
输出显然是
42
。注意,我们的语言还没有字符串操作或简单的变量赋值。另外,能够直接使用其他函数的返回值调用函数也很好。这需要某种排列或优先机制。此外,该语言还需要更好的批处理错误报告功能(它已经支持该功能)。编写按名称发送到命令的脚本相当容易。下面是一个简单的例子:
#!/usr/bin/env perl
use strict;
use warnings;
# take the command name off the @ARGV stack
my $command_name = shift;
# get a reference to the subroutine by name
my $command = __PACKAGE__->can($command_name) || die "Unknown command: $command_name\n";
# execute the command, using the rest of @ARGV as arguments
# and print the return with a trailing newline
print $command->(@ARGV);
print "\n";
sub add {
my ($x, $y) = @_;
return $x + $y;
}
sub subtract {
my ($x, $y) = @_;
return $x - $y;
}
这个脚本(比如它的名字叫myscript.pl
)可以像这样调用
$ ./myscript.pl add 2 3
或
一旦您使用了它一段时间,您可能会想进一步使用它,并为这种事情使用一个框架。有几种可用的方法,例如,或者您可以采用上面显示的逻辑,并根据需要进行模块化。创建按名称发送到命令的脚本相当容易。下面是一个简单的例子:
#!/usr/bin/env perl
use strict;
use warnings;
# take the command name off the @ARGV stack
my $command_name = shift;
# get a reference to the subroutine by name
my $command = __PACKAGE__->can($command_name) || die "Unknown command: $command_name\n";
# execute the command, using the rest of @ARGV as arguments
# and print the return with a trailing newline
print $command->(@ARGV);
print "\n";
sub add {
my ($x, $y) = @_;
return $x + $y;
}
sub subtract {
my ($x, $y) = @_;
return $x - $y;
}
这个脚本(比如它的名字叫myscript.pl
)可以像这样调用
$ ./myscript.pl add 2 3
或