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

I want to delete those .txt files in a folder that has <=3 of ">" character (starts as header). As a beginner in perl scripting, I have used:

use strict; use warnings; while ( my $file = glob '*.txt' ) { open my $fh, '<', $file; my $headers = grep /^>/, <$fh>; if ($headers <= 3) { unlink $file; } }

After executing it won't delete any files and won't show any errors either.

What am I doing wrong?

Replies are listed 'Best First'.
Re: Whats wrong in this code?
by hippo (Archbishop) on Nov 18, 2016 at 13:40 UTC
    What am I doing wrong?
    • Your glob may not match any files
    • Your script may not be opening any matched files
    • Your grep regex may not match what you want it to
    • Your unlink may be failling

    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"; } }
      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.

Re: Whats wrong in this code?
by LanX (Saint) on Nov 18, 2016 at 12:01 UTC
    Your description is unclear, do you want to count the lines starting with > ?

    that's what your regex does, its not counting all > in the file.

    Debugging hints :

    • try dumping what $headers holds.
    • put sample data after __DATA__ and use DATA as file handle

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Whats wrong in this code?
by GotToBTru (Prior) on Nov 18, 2016 at 12:42 UTC

    Use tr instead of grep to count the '>' in a string.

    Update: or use the command line grep instead:

    Update: Nevermind ;)

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      I can imagine how to use tr/// instead of m//, but how do you want to replace grep with tr?

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        tr/// returns the number of substitutions made, i.e. 0 (or false) if the searched character wasn't found, and something true otherwise. So it could more or less work to replace a grep in the specific use of grep in the original post, but you can't use a regex to specify that the ">" character should be at the beginning of the line.
Re: Whats wrong in this code?
by Anonymous Monk on Nov 18, 2016 at 11:59 UTC
    Add Error checking like use autodie qw' open unlink '; Close file Handle before try unlink

      Depends.

      qwurx [shmem] ~> touch foo.txt qwurx [shmem] ~> perl -Mautodie=open,unlink -le '$_=pop;open "<", $_; +unlink $_' foo.txt qwurx [shmem] ~> ls foo.txt ls: cannot access foo.txt: No such file or directory

      AFAIK...

      • Linux: Unlinking a file doesn't break the relation between an open file handle and the file system space allocated for the file; nor does holding an open file handle prevent unlink from succeeding, which is a directory related operation. The file's blocks are open to reallocation after the last reference to the file container is gone
      • Windows: Can't delete a busy file (the OS is aware of open file handles pointing to it)
      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        Depends....

        Yes, there exist differences between the platforms

        And yes, the portable practice is preferable because it is portable

        And yes, error checking will let you know if there are errors

Re: Whats wrong in this code?
by abhikalrt53 (Novice) on Nov 18, 2016 at 12:20 UTC
    yes.. count the lines that starts with >.. sorry