Perl - 在模式匹配中使用未初始化的值$ _

时间:2015-04-30 13:56:17

标签: perl grep

我在查找如何在perl脚本中抑制/纠正导致警告"Use of uninitialized value $_ in pattern match"的原因时遇到了一些麻烦。

以下是生成警告的单元测试脚本

Interview.t

#!/usr/bin/perl

use warnings;
use strict;
use Test::More tests => 16;
use v5.10;
use Data::Dumper;

require_ok('Interview');

# Test URLs
my $test_urls = {
    'http://www.reddit.com'  => 1,
    'hurrdurrimasheep'       => 0
};

my $interview = new Interview;

# Test $interview to see if it's an instance of our 'Interview' class
isa_ok($interview, 'Interview');

# Test to see if $interview has the member functions 'a' and 'b'
can_ok($interview, qw/a b/);

eval { $interview->a; };

cmp_ok($@, '=~', /Oh boy, thats blown to bits/, 'Interview->a dies as expected when no url argument is given.');

eval { $interview->b; };

cmp_ok($@, '=~', /Rubbish/, "Interview->b dies as expected when there are no results in Interview->{'r'}");

# Test to see if the return value from $interview's 'a' member function 
# will either be '1' or '0'.
foreach my $url (keys $test_urls) {
    cmp_ok($interview->a($url), 'eq', $test_urls->{$url}, "Got expected result for Interview->a($url): [ $test_urls->{$url} ]");
}

open(STDOUT, ">/dev/null") || die "Can't redirect stdout";
my $interview_url_results = $interview->b;
close(STDOUT);

my $num_test_urls      = keys scalar $test_urls;
my $num_interview_urls = keys scalar $interview_url_results;

cmp_ok($num_test_urls, '==', $num_interview_urls, "Got expected number of urls returned from Interview->b(): [ $num_test_urls ]");

# The following test ensures that the Interview->b() member function
# is storing the codes returned from Interview->a() correctly.

foreach my $url (keys $interview_url_results) {
    my $expected_code = $test_urls->{$url};
    my $returned_code = $interview->{'r'}->{$url};

    cmp_ok($expected_code, '==', ($returned_code == 200 ? 1 : 0), "Got expected http code from Interview->b() for $url");
}

done_testing;

Interview.pm

package Interview;

use strict;
use warnings;

sub new {
    my $c = shift;

    return bless {}, $c;
}

sub a {
    my ($self, $u) = @_;
    die 'Oh boy, thats blown to bits' if ! $u;

    my $c = "curl -sL -w '%{http_code} %{url_effective}' '$u' -o /dev/null";
    `$c` =~ /^(\d+)\s/;
    $self->{'r'}->{$u} = $1;

    return $self->{'r'}->{$u} == 200 ? 1 : 0;
}

sub b {
    my $self = shift;
    die 'Rubbish' if ! $self->{'r'};

    foreach my $u (keys %{$self->{'r'}}) {
       print "Results: $u -- $self->{'r'}->{$u}\n";
    }
    return $self->{'r'};
}

1;

输出:

[  ¯\_(ツ)_/¯ ~/Development/Interview ]: perl Interview.t
1..10
ok 1 - require Interview;
ok 2 - The object isa Interview
ok 3 - Interview->can(...)
Use of uninitialized value $_ in pattern match (m//) at Interview.t line 27.
ok 4 - Interview->a dies as expected when no url argument is given.
Use of uninitialized value $_ in pattern match (m//) at Interview.t line 31.
ok 5 - Interview->b dies as expected when there are no results in Interview->{'r'}
ok 6 - Got expected result for Interview->a(hurrdurrimasheep): [ 0 ]
ok 7 - Got expected result for Interview->a(http://www.reddit.com): [ 1 ]
ok 8 - Got expected number of urls returned from Interview->b(): [ 2 ]
ok 9 - Got expected http code from Interview->b() for hurrdurrimasheep
ok 10 - Got expected http code from Interview->b() for http://www.reddit.com

您可以在测试3和4之后立即看到警告。我在这里缺少什么?

3 个答案:

答案 0 :(得分:6)

警告来自以下一行:

cmp_ok($@, '=~', /Oh boy, thats blown to bits/, ...);

您希望将正则表达式模式传递给cmp_ok,而是传递一个值,指示$_是否包含Oh boy, thats blown to bits。警告来自于您从未向$_分配任何内容。但真正的问题是你应该使用

cmp_ok($@, '=~', qr/Oh boy, thats blown to bits/, ...);

但是仍然存在第二个问题。如果没有异常发生,$@将为undef,因此cmp_ok会在针对$@检查模式时发出警告。 like不仅在这种情况下更简单,而且在没有警告的情况下处理undef

like($@, qr/Oh boy, thats blown to bits/, ...);

答案 1 :(得分:2)

第27行是:

cmp_ok($@, '=~', /Oh boy, thats blown to bits/, 'Interview->a dies as expected when no url argument is given.');

那么,此时$@中的内容是什么?我猜 - eval { $interview->a; };正在返回undef。所以你可能想先测试它是defined

答案 2 :(得分:0)

您需要使用qr运算符来定义正则表达式而不是匹配项。如果您使用/regex/,则希望立即匹配它,但您希望运算符=~与两个参数一起使用。这一行将给出想要的结果:

cmp_ok($@, '=~', qr/Oh boy, thats blown to bits/, 'Interview->a dies as expected when no url argument is given.');