Sandy has asked for the wisdom of the Perl Monks concerning the following question:

While poking around my thoughts about this node (Strange "Useless use of constant" message: should not appear at all!), I came across the fact that the warning messages seem to ignore '0's and '1's.

Example: (please ignore the fact that this code doesn't really do anything, it's meant to illustrate the warning messages only)

#!/usr/bin/perl -w use strict; # Why the inconsistency with warning messages? my @movement = (0,1,2,3,4); my $from = ""; if (@movement == (0,1,2)) {$from = "012"} # no warning if (@movement == (1,2,3)) {$from = "123"} # 1 warning if (@movement == (2,3,4)) {$from = "234"} # 2 warnings if (@movement == (3,4,5)) {$from = "345"} # 2 warnings if (@movement == (-0,-1,-2)) {$from = "012"} # 1 warning if (@movement == (-1,-2,-3)) {$from = "123"} # 2 warnings if (@movement == (-2,-3,-4)) {$from = "234"} # 2 warnings if (@movement == (-3,-4,-5)) {$from = "345"} # 2 warnings if (@movement == (0,1,0,1)) {$from = "0101"} # no warning if (@movement == (1,2,1,2)) {$from = "1212"} # 1 warning if (@movement == (2,3,2,3)) {$from = "2323"} # 3 warnings if (@movement == (3,4,3,4)) {$from = "3434"} # 3 warnings
Does anyone know why there are different numbers of warnings? (We expect one warning per number that gets thrown away within the list)
(i.e. it doesn't give warnings for any of the numbers in the list that it throws away if that number is a zero or one).

Does this affect anything that I do in my daily coding life? Not likely, I just wanna know 'cause I'm curious.

Sandy

UPDATE

Thanks to all the posters below. It's these little details that make learning stuff all the more interesting.

Replies are listed 'Best First'.
Re: Warning messages about useless constants ignore '0's and '1's
by Juerd (Abbot) on May 10, 2004 at 23:03 UTC
Re: Warning messages about useless constants ignore '0's and '1's
by japhy (Canon) on May 10, 2004 at 23:30 UTC
    From the source (op.c):
    case OP_CONST: sv = cSVOPo_sv; if (cSVOPo->op_private & OPpCONST_STRICT) no_bareword_allowed(o); else { if (ckWARN(WARN_VOID)) { useless = "a constant"; /* don't warn on optimised away booleans, eg * use constant Foo, 5; Foo || print; */ if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT) useless = 0; /* the constants 0 and 1 are permitted as they are conventionally used as dummies in constructs like 1 while some_condition_with_side_effects; */ else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0)) useless = 0;
    Basically, if you use 0 and or 1 and, etc., Perl won't warn about the useless use of a constant.
    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
      It will never warn about 0 or 1 in void context (including in a "list" specified in scalar context), and also will not warn about any constant on the left side of a short-circuiting op. You seem to have conflated the two cases.
Re: Warning messages about useless constants ignore '0's and '1's
by ambrus (Abbot) on May 11, 2004 at 16:19 UTC

    From perldiags (5.8.1):

    Useless use of %s in void context

    ...

    This warning will not be issued for numerical con- stants equal to 0 or 1 since they are often used in statements like

    1 while sub_with_side_effects() ;
    String constants that would normally evaluate to 0 or 1 are warned about.
Re: Warning messages about useless constants ignore '0's and '1's
by ambrus (Abbot) on May 17, 2004 at 14:27 UTC