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

Hi Monks, I have searched for this and found several nodes but could not find any that appeard to answer my question.. so I pose it here:

I want to check the length of a file to determing the value of a variable for later use.

my $offline = 1 if (-s 'online.flg' gt 0);
This gets a warning Use of uninitialized value in numeric gt (>) so systematically I try defining each of the 3 elements:
my $flag_size = -s 'online.flg'; my $offline = 1 if ($flag_size > 0);
my $flag_size = -s 'online.flg'; my $zero_value = 0; my $offline = 1 if ($flag_size > $zero_value);
I even tried re-aranging my if statement:
my $zero_value = 0; my $flag_size = -s 'online.flg'; my $offline = 0; if($flag_size > $zero_value){ $offline = 1; }
All of them get the same warning.. can anyone shed any light on this?

Thanks,

___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com

Replies are listed 'Best First'.
Re: Use of uninitialized value in numeric gt (>)
by Aristotle (Chancellor) on Aug 12, 2004 at 02:23 UTC

    First of all: don't do my $foo if $cond; — that's going to surprise you when the condition is false, because my has both compile-time and runtime implications, but the conditional is suppressing the runtime effect.

    In this particular case, you don't need an if either — a condition returns a boolean value. So you can just say

    my $offline = ( -s 'online.flg' ) > 0;

    and later do something like if( $offline ) just fine.

    Actually, you don't even need to have the comparison, because if -s returns 0, that's false, and if it returns a value greater than zero, that is interpreted as true. You could just say

    my $offline = -s 'online.flg';

    and using $offline as a boolean would do the right thing.

    Finally, all that said and done — I can't reproduce your problem with an existing file. -s returns undef for non-existent ones, though. Is that the source of your problem?

    Makeshifts last the longest.

      Finally, all that said and done — I can't reproduce your problem ...

      Not surprising: the OP's warning specifically says numeric gt (>), not string gt. I suspect that the warning is coming from a different line of code.

      Hugo

Re: Use of uninitialized value in numeric gt (>)
by Zaxo (Archbishop) on Aug 12, 2004 at 02:19 UTC

    Does the file exist with that relative path from where the script runs? You can test that at runtime with -f. You may want to give an absolute path so that you can run from anywhere.

    After Compline,
    Zaxo

Re: Use of uninitialized value in numeric gt (>)
by YuckFoo (Abbot) on Aug 12, 2004 at 22:54 UTC
    The problem is -s returns undef if the file does not exist. Then undef causes the 'uninitialized' warning. One fix for this is to set $flag_size to 0 when -s returns undef:

    my $flag_size = -s 'online.flg' || 0;

    YuckDef

      Thankyou, and everyone who replied.

      I will now go and check but I recon your right. The file did not exist.

      I will check out -f as all I really want to do is know if a file exists I dont really care what size it is if it does exist.

      Ideally Id like to do this:

      my $offline = -f 'online.flg';
      ..with the hope that it will return false if the file doesn't exist (without raising a warning) - thus achieving my aim.

      Thanks Monks!

      ___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com