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

Hi I'm trying to print to a file from File::Find but can't figure out why the file isn't being created.
use strict; use warnings; use File::Find; find(\&wanted, "c:\\"); sub wanted { if (/.zip$/) { print "$File::Find::name\n"; open (FH, ">>test.txt"); print FH "$File::Find::name\n"; close (FH); } }

Replies are listed 'Best First'.
Re: File::Find and File Handlers
by ikegami (Patriarch) on May 11, 2010 at 17:25 UTC

    First of all, it's a file handle (holds a resource), not a handler (since it doesn't handle anything).

    If you can't figure out why the file isn't getting created, it's because you didn't check what error open returned.

    I don't know if you realize that you've instructed Perl to create a test.txt file in every directory that has a .zip file. File::Find's no_chdir option would be useful if you meant to create a single test.txt file.

      Thanks, that explains a lot.
Re: File::Find and File Handlers
by Anonymous Monk on May 11, 2010 at 17:24 UTC
    Because 1) You aren't error checking and 2) your current directory is not what you think it is
Re: File::Find and File Handlers
by hotpelmen (Scribe) on May 11, 2010 at 17:29 UTC
    You are giving relative path to the file. It will be created in the same folder where the zip file resides.
Re: File::Find and File Handlers
by ww (Archbishop) on May 11, 2010 at 17:36 UTC
    Works for me under *n*x (modification at your line 5: search dir changed to "./"); sted "c:\\);"

    But, I have vague recollection that (some versions of ?) W32 can be configured so that an ordinary user (non-admin) cannot write to the root directory.

    If you're getting a list of .zip files to the console from your script, then I would check to see if 'doze will let you write to c:\.