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

Hi Monks
I have this code in my program generating warnings-erros like:
Unsuccessful stat on filename containing newline at myperl.pl line 193 +, <$in> chunk 1. Use of uninitialized value in numeric gt(>) at myperl.pl line 193, <$i +n> chunk 1.

I don't understand where or how I could initialize something here to fix this problem.

open my $in, '<', "$file" or die $!; { local $/ = \$maxsize; while ( my $record = <$in> ) { # -s returns file size next if -s $record > $new_max; }

This line:
next if -s $record > $new_max;

Is where the warning gets generated, can I set a value if its returning null somewhere in this chunk of code?
Any Help!!
Thanks!

Replies are listed 'Best First'.
Re: Unsuccessful and Uninitialized Value
by davorg (Chancellor) on Jun 05, 2006 at 16:09 UTC

    I think that the error message is pretty clear here.

    Unsuccessful stat on filename containing newline

    You're reading the filename from a file. Each record that you read from the file has a newline on the end. You need to remove that before using it.

    I'd be interested in hearing ideas on how that error message could be made clearer.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I'd be interested in hearing ideas on how that error message could be made clearer.

      "...(maybe you forgot to chomp?)"

      ;-)

      Update: Looking closer at the OP's code, chomp is not the problem of course. The problem is the use of $/ which ikegami has noticed further down. But I still like my (joking) error message addition, as it sounds similar to the non-existant package/method error :)

        I did use chomp, and it still didn't work.
Re: Unsuccessful and Uninitialized Value
by ikegami (Patriarch) on Jun 05, 2006 at 16:25 UTC

    The first lesson of debugging should be: If you get an error performing an operation, make sure the arguments you are using are the ones you think you are using.

    In other words, start by making sure you are using the file name you think you are using.

    In this case, the error message even gives you a major hint that the file name you are using is not the one you meant to use. Does the file name you wish to use really have a linefeed in it? No.

    The problem is that when you read in a line from a file, the linefeed is not removed for your. Use chomp($record); to remove the trailing linefeed. (In effect, this extracts the file name from the $record.)

    Update: Actaully, your problem is worse than that. You want to read lines from file $file, right? If so, get rid of local $/ = \$maxsize;. If not, then you have more work to do, because $record contain a partial file name, a single file name or multiple file names, but you assume it contains a single file name.

      Yes, because it could be one or more file names.
      Any idea on how I could fix this problem?

        What's "it"? The file or each line?

        If it's one file name per line, then remove local $/ = \$maxsize; and add chomp($record);.

        If there can be more than one file name per line, what seperates them?

Re: Unsuccessful and Uninitialized Value
by bobf (Monsignor) on Jun 05, 2006 at 16:19 UTC
Re: Unsuccessful and Uninitialized Value
by ahmad (Hermit) on Jun 05, 2006 at 18:50 UTC

    i dont think you can use -s to find the size of a line , you can use it to check the file size

    # loop through my files foreach my $file ( @files ) { my $size = -s "fullpathtofile/$file"; next if $size > $allowed_size; # Do Some Proccessing for the file if it's not bigger than the allowed + size }

    HTH

Re: Unsuccessful and Uninitialized Value
by jpeg (Chaplain) on Jun 05, 2006 at 20:02 UTC
    Some crude debugging would help. Check to see if the file exists before you stat it, and establish a default value: The uninitialized warning is generated because $new_max is not a number yet.

    Here's how I'd do it:

    #!/usr/bin/perl use warnings; use strict; my $largestsize = 0; my $size; my $file = "filelist"; open (FILE, "$file") or die $!; while (<FILE>) { chomp; if ( -e $_ ) { $size = (stat($_))[7]; if ($size > $largestsize) { $largestsize = $size; } } } print ("$largestsize \n");
    --
    jpg