in reply to Whats wrong in this code?

What am I doing wrong?

That's just 4 possibilities and you won't know which is the real problem until you add some diagnostics to your script. eg (untested):

use strict; use warnings; while ( my $file = glob '*.txt' ) { print "Debug: found file $file\n"; open my $fh, '<', $file or die "Cannot open file $file: $!"; my $headers = grep /^>/, <$fh>; if ($headers <= 3) { unlink $file or die "Cannot unlink file $file: $!"; } else { print "Debug: file $file only has $headers headers - skipping\ +n"; } }

Replies are listed 'Best First'.
Re^2: Whats wrong in this code?
by abhikalrt53 (Novice) on Nov 19, 2016 at 06:35 UTC
    But I get this error:

    Debug: found file 1.txt

    Debug: file 1.txt only has 4 headers - skipping

    Debug: found file 2.txt

    Cannot unlink file 2.txt: Permission denied at 1.pl line 11, <$fh> line 6.