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

Hi Monks!
I am getting stuck trying to use the code to only "push" the files if the size matches the criteria inside of the "grep", but instead it is giving me this error:
Warning: Use of "-s" without parentheses is ambiguous at test.pl line 19. Unterminated <> operator at test.pl line 19.
Here is the code:
#!usr/bin/perl use warnings; use strict; use Data::Dumper; use File::Slurp qw(read_dir); use File::Basename; my @files; my @directories = qw( dir/dir1 dir/dir2 ); my $min_size = 1024; #1K my $max_size = 10240; #10MB my $c; for my $d (@directories) { $c++; push @files , grep { -f && -s >= $min_size && -s <= $max_size "$d +/$_" } read_dir($d); } foreach my $file(@files) { print "\n$file\n"; }
Thanks for the Help!

Replies are listed 'Best First'.
Re: Get file min and max size help!
by SuicideJunkie (Vicar) on Aug 29, 2011 at 14:24 UTC

    Perl says the -s is confusing. It also says "Unterminated <> operator". Look for where you've got a -s and an < together, and you should be able to trim it down to:

    C:\temp>perl -e "print -s <= 2" Warning: Use of "-s" without parentheses is ambiguous at -e line 1. Unterminated <> operator at -e line 1.

    Go and add some disambiguating brackets, and everything is happy. For example

    C:\temp>perl -e "print -s($_) <= 2" 1

Re: Get file min and max size help!
by jwkrahn (Abbot) on Aug 29, 2011 at 18:13 UTC
    push @files , grep { -f && -s >= $min_size && -s <= $max_size "$d +/$_" } read_dir($d);

    There is no operator between -s <= $max_size and "$d/$_", and you can't modify the return value of grep anyways.

    You are stating the same file three times when you only need to stat it once.

    You need something like either:

    push @files, grep { -f && -s _ >= $min_size && -s _ <= $max_size } + read_dir( $d, prefix => 1 );

    Or:

    push @files, map { -f "$d/$_" && -s _ >= $min_size && -s _ <= $max +_size ? "$d/$_" : () } read_dir( $d );
Re: Get file min and max size help!
by roboticus (Chancellor) on Aug 29, 2011 at 14:26 UTC

    Try adding parenthesis, as indicated by the error message. I've not tried it, but I'd suggest starting with:

    push @files , grep { -f && (-s >= $min_size) && (-s <= $max_size) +} map { "$d/$_" } read_dir ($d);

    Note: I've added the map bit, as it looks like that's what you were wanting.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Still getting the same issue message:
      Warning: Use of "-s" without parentheses is ambiguous at test.pl line 19. Unterminated <> operator at test.pl line 19.

        Never mind ... apparently today I'm an idiot...

        Reading the documentation reveals that -s doesn't return a number. Instead, it indicates whether has nonzero size. If you need to know the file size, you'll have to use a suitable function to read the file size, and *then* compare it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Get file min and max size help!
by blue_cowdawg (Monsignor) on Aug 29, 2011 at 14:24 UTC

    Based on what you posted, this:

        push @files , grep { -f && -s >= $min_size && -s <= $max_size "$d +/$_" } read_dir($d);

    becomes:

    push @files , map { "$d/" . $_} grep { -f && ( -s >= $min_size) && (- +s <= $max_size) } read_dir($d); #this is untested.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Get file min and max size help!
by Perlbotics (Archbishop) on Aug 29, 2011 at 14:36 UTC

    Maybe this modification? I've assumed, that you'll need full path/file-names (see read_dir() in File::Slurp):

    ... push @files, grep { -f and -s $_ >= $min_size and -s $_ <= $max_ +size } read_dir( $d, prefix => 1 ); ...
    Update: BTW: Instead of incrementing $c within the loop, you can have the same result by simply using:
    ...} # loop my $c = @directories; ...