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

I'm trying to do the first exercise in the Intermediate Perl book. It calls for taking filenames from the command line and parsing them with grep to determine which are less then 1000 bytes and then doing some transformation on the strings with map. For now I'm ignoring the "from the command line" part and just reading the files in from a directory.

I haven't reached the map part yet because I'm hung up on the grep. This is the code I have:
#!/usr/bin/perl use warnings; use strict; my $dh, $dir = "/home/msnyder/bin/etc_copy/"; my $dh; opendir($dh, $dir) or die "Unable to open directory $dir: $!."; my @files = grep -s $_ < 1000, readdir($dh); foreach (@files) { print $_ . "\n"; }

I don't think it is actually getting the size of each file because I'm getting Use of uninitialized value $_ in numeric lt (<) at ./int_perl1-1.pl line 10.

Initially, I was seeing the same error that bluethundr was getting in another thread about the filename containing a newline. I applied the substitute fix that was suggested but it didn't solve the issue. To be honest, I'm not seeing that anymore so I don't know what was causing it. I'm only seeing the error pointed out above.

After a long list of identical errors it then prints out the file names.

I've tried substituting the grep and subsequent foreach loop with a foreach loop on the directory handle:

foreach (readdir($dh)) { my $filesize = -s $_; print $_ . " " . $filesize . "\n"; }

But that just gives me Use of uninitialized value $filesize in concatenation (.) or string at ./int_perl1-1.pl line 19. outputting it before each filename is printed.

I'm not sure what other information I should be providing. If I'm missing something that will be helpful let me know.

Replies are listed 'Best First'.
Re: filesize not returned?
by ikegami (Patriarch) on Jul 13, 2010 at 03:58 UTC

    Basic debugging would have answered your question.

    Were you to check $! after -s returned undef, it would tell you file not found.

    Were you to check the path you were passing to -s, you would have noticed how it was incorrect.

    readdir returns file names (not paths), so you're asking Perl to get the size of file (as in ./file, which doesn't exist) instead of /home/msnyder/bin/etc_copy/file

Re: filesize not returned?
by jwkrahn (Abbot) on Jul 13, 2010 at 06:12 UTC
    use warnings; use strict; my $dh, $dir = "/home/msnyder/bin/etc_copy/"; my $dh; opendir($dh, $dir) or die "Unable to open directory $dir: $!.";

    You should have got an error message there:

    Parentheses missing around "my" list at ./int_perl1-1.pl line 6.
    "my" variable $dh masks earlier declaration in same scope at ./int_perl1-1.pl line 7.
    Global symbol "$dir" requires explicit package name at ./int_perl1-1.pl line 6.

    You want to change that to:

    use warnings; use strict; my $dir = "/home/msnyder/bin/etc_copy/"; opendir(my $dh, $dir) or die "Unable to open directory $dir: $!.";