首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回字符串的子例程警告“无效上下文中常量的无用使用”。

返回字符串的子例程警告“无效上下文中常量的无用使用”。
EN

Stack Overflow用户
提问于 2018-06-18 10:58:01
回答 2查看 488关注 0票数 2

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

代码语言:javascript
复制
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行。

如果Perl子例程是表达式,则返回执行的最后一条语句的值,而且这种技术以前是有效的。我无法重现Perl5.10.1或5.18.2版本的问题。

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

代码语言:javascript
复制
# 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版本中的错误吗?

EN

回答 2

Stack Overflow用户

发布于 2018-06-18 11:10:12

Void是这样一个上下文,在这个上下文中,没有任何东西可以使用返回的内容。

当使用一个空的原型定义子时,就会出现警告:

代码语言:javascript
复制
use warnings;
sub blurb () { q(some string) }
blurb();

您能显示NetApp/Dox/FlashCache.pm第27行吗?

票数 5
EN

Stack Overflow用户

发布于 2018-07-05 08:35:12

哦,不,我们终于在一些客户服务器上找到了错误信息“常量的无用使用”的原因。这个问题是由配送链中的一个错误引入到客户的路上的。

我们发送的代码(例如,减少):

代码语言:javascript
复制
#!/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
}

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

代码语言:javascript
复制
#!/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中的附加行。

结论

除了修复分发链之外,通过将文本存储在变量中并在稍后返回该变量,或者省略最后一个分号,这些代码将变得更加健壮,后者将在编译时抛出一个明显的错误,并将节省调试时间。

代码语言:javascript
复制
#!/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;
}

对于第一个错误代码示例,很抱歉,谢谢您的评论!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50907938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档