我有一个函数返回一个数组,我想从该数组中得到第一个项目,而不是为数组声明一个变量.基本上,它应该是这样的:
functionReturningArray()[1]
除此之外不起作用.
我真的不想浪费空间来声明整个阵列,因为我不需要它,而且我宁愿不浪费额外的代码行.无论如何要在一行中做到这一点?
请解释这个明显不一致的行为:
$a = b, c;
print $a; # this prints: b
$a = (b, c);
print $a; # this prints: c
Run Code Online (Sandbox Code Playgroud) 看了这个问题,并且这个问题上的分歧my $var和my ($var),我仍然无法理解为什么数据::翻车机模块中从以下代码摘录使用括号.这些问题的答案中描述的差异似乎都不适用于此.
my($s) = {
level => 0, # current recursive depth
indent => $Indent, # various styles of indenting
# a bunch of other settings removed for brevity's sake
deparse => $Deparse, # use B::Deparse for coderefs
noseen => $Sparseseen, # do not populate the seen hash unless necessary
};
Run Code Online (Sandbox Code Playgroud)
我用一个小脚本测试了它,我觉得在声明这个my ($s)或者之间没有任何区别my $s.在这两种情况下,它都是哈希的标量引用,据我所知.
我错过了什么吗?
请帮助我理解以下片段:
my $count = @array;my @copy = @array;my ($first) = @array;(my $copy = $str) =~ s/\\/\\\\/g;my ($x) = f() or die;my $count = () = f();print($x = $y);print(@x = @y);我正在研究一些示例代码,我试图弄清楚为什么它与$ args周围的括号一起工作.没有它,我没有得到价值.
sub random_dice{
my ($args) = @_;
my $number_of_rolls = $args->{number_of_rolls} || 6;
...
}
# I don't understand why it works with the brackets around $args
my $r = random_dice({number_of_rolls=>5});
Run Code Online (Sandbox Code Playgroud) 我看过这篇SO帖子,看到了这个部分
my @array = ('a', 'b', 'c');
my $variable = @array; # 3 size of @array
my ($variable) = @array; # 'a' $array[0]
Run Code Online (Sandbox Code Playgroud)
指定@arrayto 的第一个元素my ($variable).
我对以下继承的代码感到有点困惑.具体来说就是
my ($ptBatchRec);
被宣布为指针?
如果我删除括号会怎么样?
my $DBHandle = shift; # The ICS database handle
my $ptBatchStruct = shift; # -> batch structure (hash)
my ($FiscalYear, $AccountType, $BatchType, $PaymentType, %Args) = @_;
my $DBIsLocked = 0; # Database is locked flag
my ($ptBatchRec); # -> batch record
.
. …Run Code Online (Sandbox Code Playgroud)