相关疑难解决方法(0)

如何从Perl中返回数组的函数中获取第一项?

我有一个函数返回一个数组,我想从该数组中得到第一个项目,而不是为数组声明一个变量.基本上,它应该是这样的:

functionReturningArray()[1]

除此之外不起作用.

我真的不想浪费空间来声明整个阵列,因为我不需要它,而且我宁愿不浪费额外的代码行.无论如何要在一行中做到这一点?

perl

5
推荐指数
1
解决办法
411
查看次数

=和,Perl中的运算符

请解释这个明显不一致的行为:

$a = b, c;
print $a; # this prints: b

$a = (b, c);
print $a; # this prints: c
Run Code Online (Sandbox Code Playgroud)

perl

4
推荐指数
2
解决办法
281
查看次数

为什么Dumper模块会在其设置哈希的分配周围放置parens?

看了这个问题,并且这个问题上的分歧my $varmy ($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.在这两种情况下,它都是哈希的标量引用,据我所知.

我错过了什么吗?

perl

4
推荐指数
1
解决办法
81
查看次数

标量 vs 列表赋值运算符

请帮助我理解以下片段:

  • 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);

perl assignment-operator

4
推荐指数
1
解决办法
253
查看次数

取消引用传递给子例程的哈希引用

我正在研究一些示例代码,我试图弄清楚为什么它与$ 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)

perl

1
推荐指数
1
解决办法
83
查看次数

是我的($ ptBatchRec); 一个指针?

我看过这篇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)

perl

1
推荐指数
1
解决办法
56
查看次数

标签 统计

perl ×6

assignment-operator ×1