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 如何在其他内容中使用给定的(){}?_Perl - Fatal编程技术网

Perl 如何在其他内容中使用给定的(){}?

Perl 如何在其他内容中使用给定的(){}?,perl,Perl,我试图以动态的方式传递参数。我想使用Perl函数given(){},但由于某些原因,我无法在其他任何内容中使用它。这是我的 print(given ($parity) { when (/^None$/) {'N'} when (/^Even$/) {'E'} when (/^Odd$/) {'O'} }); 现在我知道我可以在这之前声明一个变量,并在print()函数中使用它,但我正在努力使代码更简洁。同样的原因,我不使用复合的if-then-else语句。如果有帮助,这里

我试图以动态的方式传递参数。我想使用Perl函数
given(){}
,但由于某些原因,我无法在其他任何内容中使用它。这是我的

print(given ($parity) {
   when (/^None$/) {'N'}
   when (/^Even$/) {'E'}
   when (/^Odd$/)  {'O'}
});
现在我知道我可以在这之前声明一个变量,并在
print()
函数中使用它,但我正在努力使代码更简洁。同样的原因,我不使用复合的
if-then-else
语句。如果有帮助,这里是错误

syntax error at C:\Documents and Settings\ericfoss\My Documents\Slick\Perl\tests\New_test.pl line 22, near "print(given"
Execution of C:\Documents and Settings\ericfoss\My Documents\Slick\Perl\tests\New_test.pl aborted due to compilation errors.

不能将语句放入表达式中

print( foreach (@a) { ... } );  # Fail
print( given (...) { ... } );   # Fail
print( $a=1; $b=2; );           # Fail
尽管
do
可以帮助您实现这一目标

print( do { foreach (@a) { ... } } );  # ok, though nonsense
print( do { given (...) { ... } } );   # ok
print( do { $a=1; $b=2; } );           # ok
但说真的,你想要一份杂烩

my %lookup = (
   None => 'N',
   Even => 'E',
   Odd  => 'O',
);

print($lookup{$parity});
甚至

print(substr($parity, 0, 1));

哦,我的天啊,我想我在给定的声明上卖得太多了,出于某种原因,我想不起做一个杂烩。。。谢谢你的回答!