lanti
lanti

Reputation: 549

Subroutine returning a string warns about "Useless use of a constant in void context"

I have a simple function blurb in a module that returns some text

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

On one server we are getting occasional warnings:

Useless use of a constant ("Checks several metrics of "...) in void context at .../lib/Il/NetApp/Dox/FlashCache.pm line 27.

A Perl subroutine returns the value of the last statement executed if it is an expression, and this technique has worked before. I can't reproduce the problem with Perl v5.10.1 or v5.18.2.

The site having these warnings is running 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)

Could this be a bug in a specific Perl version?

Upvotes: 2

Views: 713

Answers (2)

lanti
lanti

Reputation: 549

Oh no, finally we have found the reason for that error message "Useless use of a constant" on some customers server. The problem had been introduced by a bug in the distribution chain on the way to the customer.

The code we have sent was (example, reduced):

#!/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
}

The code which had actually been run on the customers server and which caused that error was slightly different:

#!/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
}

The cause for the error-message was the additional line in the blurb-sub.

Conclusions

Beside of fixing the distribution chain future code will be made a bit more robust by either storing the text in a variable and returning that variable later or leaving out the last semicolon which would have thrown a clear error during compile-time and would have saved hours of debugging.

#!/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;
}

Sorry for the first miss-leading code-example and thanks for all your comments!

Upvotes: 1

choroba
choroba

Reputation: 241848

Void context is a context where there's nothing to consume what's been returned.

The warnings occurs where the sub is defined with an empty prototype:

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

Can you show NetApp/Dox/FlashCache.pm line 27?

Upvotes: 5

Related Questions