Che*_*eso 5 arrays perl strict multidimensional-array
如果use strict;
被注释掉的话,我有几行代码可以工作.但是,我不希望因为一个小部分而禁用整个脚本.
我需要重新编码,或以某种方式use strict;
暂时禁用,然后重新启用它.第一个选项更现实,但我不知道如何更改代码以在严格模式下工作.
my ($ctc_rec_ref) = get_expected_contacts($ctc,$fy);
my @expected_ctc_rec = @$ctc_rec_ref;
print $expected_ctc_rec[0][0]."\n";
print $expected_ctc_rec[0][1]."\n";
print $expected_ctc_rec[0][2]."\n";
sub get_expected_contacts
{
my (@ctc_rec,$i) = ((),0);
$STMT = "SELECT DISTINCT field1, field2, field3 FROM table WHERE field4 = ? AND field5 = ? AND field6 = 'E'";
$sth = $db1->prepare($STMT); $sth->execute(@_);
while(@results = $sth->fetchrow_array())
{
push @{ $ctc_rec[$i] }, $results[0];
push @{ $ctc_rec[$i] }, $results[1];
push @{ $ctc_rec[$i] }, $results[2];
$i++;
}
return (\@ctc_rec);
}
Run Code Online (Sandbox Code Playgroud)
use strict;
启用:在./return5.pl第49行使用"strict refs"时,不能使用字符串("0")作为ARRAY引用.
(线49: push @{ $ctc_rec[$i] }, $results[0];
)
随着use strict;
禁用:
1468778
04/01/2011
30557
Run Code Online (Sandbox Code Playgroud)
如何重写此代码以使其像禁用严格模式一样工作?如果不可能,可以use strict;
暂时禁用,然后在脚本中为这段短代码重新启用?
cjm*_*cjm 20
问题是
my (@ctc_rec,$i) = ((),0);
Run Code Online (Sandbox Code Playgroud)
不会做你认为它做的事情.它意味着相同
my @ctc_rec = (0);
my $i;
Run Code Online (Sandbox Code Playgroud)
strict
正在做它的意图并抓住你的错误.试着写:
my @ctc_rec;
my $i = 0;
Run Code Online (Sandbox Code Playgroud)
代替.那应该摆脱错误.
在这种情况下,还有另一种方法可以消除错误,同时大大简化代码:使用selectall_arrayref.
sub get_expected_contacts
{
return $db1->selectall_arrayref(
"SELECT DISTINCT field1, field2, field3 FROM table WHERE field4 = ? AND field5 = ? AND field6 = 'E'",
undef, @_
);
}
Run Code Online (Sandbox Code Playgroud)
如果你真的故意做了一些被禁止的事情strict
(但知道你在做什么),你可以strict
在本地禁用:
use strict;
# this code is strict
{
no strict;
# some code that is not strict here
}
# strict is back in effect now
Run Code Online (Sandbox Code Playgroud)
但是你应该永远不要那样做,直到你完全理解strict
抱怨的是什么,以及为什么在这种情况下可以做到这一点.最好只禁用strict
你必须的部分.例如,您可以说no strict 'refs';
允许符号引用而不禁用其他内容strict
.(注意:相同的技术适用于警告 pragma,您也应该使用它.)
odr*_*drm 10
问题在于@ctc_rec和$ i的声明.试试这个,你的代码应该停止给出严格的错误:
更换:
my (@ctc_rec,$i) = ((),0);
Run Code Online (Sandbox Code Playgroud)
附:
my @ctc_rec;
my $i = 0;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1586 次 |
最近记录: |