Why does Perl warn of a "useless constant 1" when using bigint?

I wrote a module as part of my application when I noticed the results of syntax checking in a warning about the useless use of a constant (1). Why is this?

The constant is a mandatory 1 at the end of the module, which is usually ignored by warnings, as perldoc perldiag says:

This warning will not be issued for numeric constants equal to 0 or 1, as they are often used in expressions such as

 1 while sub_with_side_effects(); 

(There is probably an even better source for this. In the end, all 1 at the end of the files are completely desirable and should not be warned about it.)

But a warning is generated even for almost empty modules, if they use bigint .

 package Foo; use bigint; 1; 

The following warning is issued for this simple file syntax check:

 $> perl -Wc Foo.pm Useless use of a constant (1) in void context at Foo.pm line 5. Foo.pm syntax OK 

I could not find the link to bigint and the warning message, except Put long hexadecimal numbers in sqlite , but this really does not concern my problem, I think.

My Perl v5.14.4 on Cygwin with bigint 0.36.

+8
perl bigint perl-module perlsyn
source share
3 answers

There are two questions here.

  • Why use bigint; 1; use bigint; 1; warns in a void context?
  • Why is a constant executed in the void context in the first place?

 $ perl -c -we'1 while sub_with_side_effects();' -e syntax OK $ perl -c -we'use bigint; 1 while sub_with_side_effects();' Useless use of a constant (1) in void context at -e line 1. -e syntax OK 

Why use bigint; 1; use bigint; 1; warns in a void context?

use bigint; sets the callback called when the parser encounters a constant literal, and the value returned by the callback is used instead of a constant. Thus, under use bigint; , 1 no longer just just 0 or 1 .

But you are not doing anything wrong, so this warning is false. You can get around this using () or undef instead of 1 .

 undef while sub_with_side_effects(); 

If I do not need to use it in my code base, I would prefer the following:

 while ( sub_with_side_effects() ) { } 

 $ cat Module.pm package Module; use bigint; 1; $ perl -c -w Module.pm Useless use of a constant (1) in void context at Module.pm line 3. Module.pm syntax OK 

Why is a constant executed in a void context?

When Perl executes a module, Perl expects the module to return a scalar value, so Perl should execute the module in a scalar context.

However, you told Perl to compile script Module.pm . When Perl runs the script, Perl does not require any values ​​to be returned, so Perl runs the script in the void context.

Using the module as a script can cause false warnings and errors, and therefore -W may pass. Check the module as follows:

 perl -we'use Module' 

In fact, you do not even need -W , since the module should already have use warnings; . All you really need is

 perl -e'use Module' 
+8
source share

-W instead of use warnings; in your module or checking modules with -c instead of perl -MFoo -e0 may show false errors. This is an example of the latter.

When you load a module normally, it is not in the void context because it checks that the result is correct.

(Note that when I try to use it with 5.20.1, -W also leads to false overload arg '..' is invalid at /usr/share/perl/5.20/Math/BigInt.pm line 155 )

+6
source share

Just leave a workaround here to avoid the warning: defining a constant with a value of 1 before using bigint :

 package Foo; use strict; use warnings; use constant PACKAGE_END => 1; use bigint; PACKAGE_END; 
0
source share

All Articles