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

I got a warning message which I can't understand.
$ perl ./a Useless use of numeric gt (>) in void context at /work/tmp/a line 4.
where script './a' goes:
#!/usr/bin/perl use warnings; my $flag= ((-M '/tmp/a') > (-M '/tmp/b')); #no warnings my $flag2= $flag and ((-M '/tmp/c') > (-M '/tmp/d')); #warning at here
My guess is that the Perl optimizer considers "-M '/tmp/c'" as just a boolean expression by itself, not a part of scalar comparison.
In real case, I want to write a condition which tests 1) existence of some file $file1. 2) when $file1 exists, test if $file1 is older than the other file($file2).
if(-f $file1 and ((-M _) > (-M $file2))){ ...
raise a waring message. How should I write in this case?
My perl version is 'v5.8.4 built for i386-linux-thread-multi'.

Replies are listed 'Best First'.
Re: Warning with file test(-M)
by borisz (Canon) on May 01, 2005 at 08:04 UTC
    The problem is that and has a lower priority as =. This means that you wrote
    ( my $flag2= $flag ) and ((-M '/tmp/c') > (-M '/tmp/d'));
    instead of
    my $flag2= $flag && ((-M '/tmp/c') > (-M '/tmp/d'));
    Boris
Re: Warning with file test(-M)
by eibwen (Friar) on May 01, 2005 at 08:28 UTC

    You can find operator precendence in perlop.

    Additionally, don't forget to use strict; You may also be interested in use diagnostics; which yielded a considerable amount of diagnostic information for your code:

    Useless use of numeric gt (>) in void context at ./tmp line 8 (#1)
        (W void) You did something without a side effect in a context that does
        nothing with the return value, such as a statement that doesn't return a
        value from a block, or the left side of a scalar comma operator.  Very
        often this points not to stupidity on your part, but a failure of Perl
        to parse your program the way you thought it would.  For example, you'd
        get this if you mixed up your C precedence with Python precedence and
        said
    
    Use of uninitialized value in numeric gt (>) at ./tmp line 7 (#2)
        (W uninitialized) An undefined value was used as if it were already
        defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
        To suppress this warning assign a defined value to your variables.

    Line numbers pertain to the following code (OP's code with addition of use strict; and use diagnostics;):

    #!/usr/bin/perl use strict; use warnings; use diagnostics; my $flag= ((-M '/tmp/a') > (-M '/tmp/b')); #no warnings my $flag2= $flag and ((-M '/tmp/c') > (-M '/tmp/d')); #warning at here