我想有条件地设置哈希键/值。我做了一些搜索,但似乎无法为我的查询找到合适的字词。谢谢!
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $q = new CGI;
my $foo = $q->cookie('Foo');
my $uri = 'https://www.google.com';
#is there a way to do this more elegantly?
print $q->redirect(-uri => $uri, -cookie => $foo) if ($foo);
print $q->redirect($uri) unless ($foo);
答案 0 :(得分:5)
print $q->redirect(
$foo ? (-uri => $uri, -cookie => $foo)
: $uri
);
答案 1 :(得分:4)
至少,我会使用conditional operator
print $foo
? $q->redirect(-uri => $uri, -cookie => $foo)
: $q->redirect($uri);
答案 2 :(得分:3)
use PerlX::Maybe;
print $q->redirect( -uri => $uri, maybe -cookie => $foo );
答案 3 :(得分:2)
仅供参考,您的$uri
可以始终使用-uri
参数指定。
始终是Enterprise"运营商"如果你想避免PerlX::Maybe
的需要(并使你的代码难以阅读):
print $q->redirect(
-uri => $uri,
( -cookie => $foo )x!! $foo,
);