Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl将参数从Bash传递到文件中_Bash_Perl_Arguments - Fatal编程技术网

Perl将参数从Bash传递到文件中

Perl将参数从Bash传递到文件中,bash,perl,arguments,Bash,Perl,Arguments,我必须在Perl文件中运行函数,并将参数传递给函数 # program.pm sub go { # I need to use arguments here # print foo # print bar } my %functions = ( go => \&go ); my $function = shift; if (exists $functions{$function}) { $functions{$function}->();

我必须在Perl文件中运行函数,并将参数传递给函数

# program.pm

sub go
{
   # I need to use arguments here
   # print foo
   # print bar
}

my %functions = (
  go  => \&go
);

my $function = shift;

if (exists $functions{$function}) {
  $functions{$function}->();
} else {
  die "There is no function called $function available\n";
}
这需要运行并通过bash传递。我需要能够随机指定参数,如下所示:

use Data::Dumper;

sub go
{
   # I need to use arguments here
   # print foo
   # print bar

   my %args = map { split /=/ } @_;

   print "In go\nArgs are: " . Dumper(\%args) . "\n";
}
$perl program.pm go foo='bar'bar='fubar'

我对Perl非常不熟悉。我在谷歌上到处搜索,一辈子都搞不懂如何正确解析这些。似乎有4种不同的方法可以做到这一点,但没有一种适合我的用例

我也试过了,但没用

$perl program.pm-e'go(foo=>“bar”)
您可以使用
require
将program.pm“包括”到“一行程序”中

shell/bash脚本

perl -e 'require "/path/program.pm" ; &go(1=>2)'
下午五时

sub go
{
   # sample subroutine

   # print subroutine parameters
   print "go-params: @_\n";
   # convert subroutine parameters into hash
   my %hash = @_;
   print "go-hash-1: $hash{1}\n"
}
# IMPORTANT: indicate proper module initialization
1;

你已经接受的答案似乎相当复杂。只需对现有代码进行几处更改,就可以实现这一点

sub go
{
   # I need to use arguments here
   # print foo
   # print bar
   print "In go\nArgs are: @_\n";
}

my %functions = (
  go  => \&go
);

my $function = shift;

if (exists $functions{$function}) {
  # Pass remaining command-line args to the called subroutine
  $functions{$function}->(@ARGV);
} else {
  die "There is no function called $function available\n";
}
我在
go()
中放入了
print()
调用(因此我知道它正在被调用),并将
@ARGV
传递给调度表中找到的子例程

您可以像调用任何其他Perl程序一样调用它

$ perl program.pm go foo=bar bar=fubar
In go
Args are: foo=bar bar=fubar

$ perl program.pm XX foo bar
There is no function called XX available
更新:在评论中,添加了以下要求:


但是如何将这些值分割成散列呢

对此有两个答案。你选择哪一个取决于你真正想做什么

如果您只想获取任何“foo=bar”字符串并将其解析为存储在散列中的键/值对,则可以使用如下代码替换
go()
子例程:

use Data::Dumper;

sub go
{
   # I need to use arguments here
   # print foo
   # print bar

   my %args = map { split /=/ } @_;

   print "In go\nArgs are: " . Dumper(\%args) . "\n";
}
然后获得以下输出:

$ perl program.pm go foo=bar bar=fubar
In go
Args are: $VAR1 = {
          'bar' => 'fubar',
          'foo' => 'bar'
        };
如果您确实在尝试解析命令行选项,那么应该使用命令行选项解析器,如

请注意,要使其正常工作,您需要传递以
--
开头的正确Unix样式选项

但该版本的输入要求要灵活得多:

$ perl program.pm go --f bar --b fubar
In go
Args are: $VAR1 = {
          'bar' => 'fubar',
          'foo' => 'bar'
        };
它会告诉您是否使用了无效的选项名

$ perl program.pm go --fu=bar --baz=fubar
Unknown option: fu
Unknown option: baz
In go
Args are: $VAR1 = {};

可能重复为什么
需要该程序而不仅仅是运行它?子程序调用上的
&
实现了什么?@DaveCross 1)Require应该使program.pl中定义的许多子程序在“一行程序”中可用。2) 在这种情况下,调用
go
子例程不需要
。这是我喜欢的风格指南。3) 你的答案是“更好的推荐风格”。我的答案是“短代码”,这使得program.pm1/中定义的子程序的使用更加复杂,但如果您只是运行该程序,那么无论如何都会定义子程序。2/调用子例程很少需要使用
&
。如果这是您首选的方法,那么您正在编写比需要更难阅读的代码。@DaveCross 1)展示您集成文件和一行程序中的子程序的方法(
-e
2)“更难阅读”-这是个人喜好的问题,可能与其他语言的解释习惯“不合适”。它使代码更易于阅读。1/没有什么可显示的。它只是工作。如果从命令行运行程序,则其所有子例程都可用。2/
&
不仅仅是个人喜好的问题<代码>&
可以改变以模糊方式调用子例程的方式。除非您理解
foo
&foo
之间的区别,否则我强烈建议您不要使用后者。但是我如何将值拆分为散列?哦。我很抱歉没有注意到你在问题中没有提到的要求:-)哈哈,对不起!我确实更喜欢你的方式,如果有一些容易解析的方法的话。
$ perl program.pm go --fu=bar --baz=fubar
Unknown option: fu
Unknown option: baz
In go
Args are: $VAR1 = {};