如何避免使用“strict”时产生的此错误?

发布于 2024-10-31 14:15:28 字数 1293 浏览 1 评论 0原文

如果 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);
}


With use strict; enabled:

无法使用字符串(“0”)作为 ARRAY 引用 而“严格裁判”在使用 ./return5.pl 第 49 行。

(第 49 行:push @{ $ctc_rec[$i] }, $results[0];


禁用 use strict; 时:

1468778 
04/01/2011 
30557

如何重写此代码,使其像禁用严格模式一样工作?如果这是不可能的,是否可以暂时禁用 use strict; ,然后重新启用脚本中的这段简短代码?

I have a couple of lines of code that work if use strict; is commented out. However, I don't want to have it disabled for the entire script just because of one small section.

I need to either recode it, or somehow disable use strict; temporarily and then re-enable it. The first option is more realistic, but I don't know how to change the code to work in strict mode.

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


With use strict; enabled:

Can't use string ("0") as an ARRAY ref
while "strict refs" in use at
./return5.pl line 49.

(line 49: push @{ $ctc_rec[$i] }, $results[0];)

With use strict; disabled:

1468778 
04/01/2011 
30557

How can I rewrite this code so that it works as if strict mode was disabled? If that is not possible, can use strict; be temporarily disabled and then re-enabled for this short piece of code within the script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-11-07 14:15:29

问题在于 @ctc_rec 和 $i 的声明。 错误:

试试这个,你的代码应该停止给出来自 strict: Replace:

my (@ctc_rec,$i) = ((),0);

With: 的

my @ctc_rec;
my $i = 0;

The problem is with the declaration of @ctc_rec and $i. Try this and your code should stop giving the error from strict:

Replace:

my (@ctc_rec,$i) = ((),0);

With:

my @ctc_rec;
my $i = 0;
沩ん囻菔务 2024-11-07 14:15:28

问题是它

my (@ctc_rec,$i) = ((),0);

并没有按照你的想法去做。这与 strict 的意思相同,

my @ctc_rec = (0);
my $i;

就是做它应该做的事情并发现你的错误。尝试写:

my @ctc_rec;
my $i = 0;

代替。这应该消除错误。

在这种情况下,还有另一种方法可以消除错误,同时大大简化代码:使用 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, @_
   );
}

如果您确实有意执行 strict 所禁止的操作(但知道自己在做什么),则可以在本地禁用 strict

use strict;

# this code is strict
{ 
  no strict;
  # some code that is not strict here
}
# strict is back in effect now

但在您了解之前切勿这样做strict 到底在抱怨什么,以及为什么在这种情况下可以这样做。最好只禁用您必须禁用的 strict 部分。例如,您可以使用 no strict 'refs'; 来允许符号引用,而不禁用 strict 执行的其他操作。 (注意:同样的技术适用于 warnings 编译指示,您也应该使用它。)

The problem is that

my (@ctc_rec,$i) = ((),0);

doesn't do what you think it does. It means the same as

my @ctc_rec = (0);
my $i;

strict is doing what it's meant to and catching your mistake. Try writing:

my @ctc_rec;
my $i = 0;

instead. That should get rid of the error.

In this case, there's another way to get rid of the error, and simplify your code considerably at the same time: use 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, @_
   );
}

If you really were intentionally doing something that was prohibited by strict (but knew what you were doing), you can disable strict locally:

use strict;

# this code is strict
{ 
  no strict;
  # some code that is not strict here
}
# strict is back in effect now

But you should never do that until you understand exactly what strict is complaining about, and why it's ok to do that in this instance. It's also better to disable only the part of strict that you have to. For instance, you can say no strict 'refs'; to allow symbolic references without disabling the other things strict does. (Note: The same technique works with the warnings pragma, which you should also be using.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文