返回字符串的子程序警告"在void上下文中无用的常量"

时间:2018-06-18 10:58:01

标签: perl

我在一个返回一些文本的模块中有一个简单的函数blurb

package Il::NetApp::Dox::FlashCache;

use strict;
use warnings;
no if $] >= 5.018, warnings => "experimental::smartmatch";

use Carp;
use Carp::Heavy;
use Data::Dumper;

use FindBin qw($Bin);
use Il::SysMon::Tools 3.2.1 qw( :debug :special_chars :text_format :help_snippets);
use Il::NetApp::Tools qw( :help_snippets ); 

# ===========================================================================
# = Texte  - ab hier wird übersetzt =
# ===========================================================================

# ===========================================================================
# Markdown Syntax in blurb, extra und examples=>txt!
#
# Verwendbare Variablen in den Texten:
#
# $VERBOSE_HINT = Hinweis -v einzusetzen
#
# ===========================================================================

sub blurb {
q{Checks several metrics of NetApps FlashCache (PAM II).}; # Line 27
}

sub extra {
<<END_EXTRA,
This plugin checks various performance counters of the NetApp-system.

A list of supported counters is printed via the `--counter` switch.

$HELP_DISCOVER_COUNTERS
END_EXTRA
} 

#
# Examples: Hier ist jeweils nur txt => zu übersetzen
# 

sub simple_examples {
    my $examples = 
        [
            {
                cmd => q{--explore=counters},
                txt => q{List all available and supported counters on the target system.}
            },
            {
                cmd => q{-z hit_percent -w 0 -c 0},
                txt => q{Monitor the hitrate for trendanalyses but do not alarm.}
            },
        ]
    ; # Ende von my $examples =
    return $examples;
}

# sub advanced_examples {
#     my $examples = 
#         [
#             {
#                 cmd => q{},
#                 txt => q{}
#             },
#         ]
#     ; # Ende von my $examples =
#     return $examples;
# }


# ===========================================================================
# = ENDE der Texte - ab hier ist nichts mehr zu übersetzen =
# ===========================================================================

1;  # return true

在一台服务器上,我们偶尔会收到警告:

  

在... / lib / Il / NetApp / Dox / FlashCache.pm第27行的void上下文中无用地使用常量(&#34;检查&#34; ...的几个指标)。

Perl子例程返回最后执行的语句的值(如果它是表达式),并且此技术之前已经有效。我不能用Perl v5.10.1或v5.18.2重现这个问题。

有这些警告的网站正在运行Perl v5.16.3

# perl --version

This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 33 registered patches, see perl -V for more detail)

这可能是特定Perl版本中的错误吗?

2 个答案:

答案 0 :(得分:5)

Void上下文是一个上下文,没有什么可以消耗已经返回的内容。

警告发生在使用空原型定义sub的地方:

use warnings;
sub blurb () { q(some string) }
blurb();

你能展示NetApp / Dox / FlashCache.pm第27行吗?

答案 1 :(得分:0)

哦,不,最终我们在某些客户服务器上找到了该错误消息“无用的常量”的原因。该问题是由分销链中的错误引入客户的。

我们发送的代码是(例如,简化的):

#!/usr/local/bin/perl -w
use warnings;
use strict;
use feature ":5.10";

say blurb();

say "\n\n\nDESCRIPTION and EXAMPLES\n\n" . extra();

sub blurb {
q{I am the blurb - print me wherever you want me to be shown.};
}


sub extra {
<<END;
Some extra documentation - spanning several lines including indents.

EXAMPLES:

    # so something
    do_something.pl -H <hostname>

Does something with hostname.

HINT: Da also lag der Hund begraben (German saying, "that's where the dog is buried" translates to "there's the fly in the ointment")
END
}

实际上在客户服务器上运行并导致该错误的代码略有不同:

#!/usr/local/bin/perl -w
use warnings;
use strict;
use feature ":5.10";

say blurb();

say "\n\n\nDESCRIPTION and EXAMPLES\n\n" . extra();

sub blurb {
q{I am the blurb - print me wherever you want me to be shown.};
my $status;
}


sub extra {
<<END;
Some extra documentation - spanning several lines including indents.

EXAMPLES:

    # so something
    do_something.pl -H <hostname>

Does something with hostname.

HINT: Da also lag der Hund begraben (German saying, "that's where the dog is buried" translates to "there's the fly in the ointment")
END
}

导致错误消息的原因是在Blurb-sub中增加了一行。

结论

除了修复分发链之外,通过将文本存储在变量中并稍后返回该变量,或者省去最后一个分号(在编译时会抛出明显的错误,并且可能会导致代码混乱),可以使将来的代码更加健壮。节省了调试时间。

#!/usr/local/bin/perl -w
use warnings;
use strict;
use feature ":5.10";

say blurb();

say "\n\n\nDESCRIPTION and EXAMPLES\n\n" . extra();

sub blurb {
q{I am the blurb - print me wherever you want me to be shown.}
}


sub extra {
    my $extra = 
<<END;
Some extra documentation - spanning several lines including indents.

EXAMPLES:

    # so something
    do_something.pl -H <hostname>

Does something with hostname.

HINT: Da also lag der Hund begraben (German saying, "that's where the dog is buried" translates to "there's the fly in the ointment")
END
return $extra;
}

对不起,第一个误导代码示例,感谢您的所有评论!