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

I have a problem when I want to copy the result of my code into a text file.
here is my code that find all files from a specific folder , then write the result
of find function into a file.txt:
use File::Find; $dir = "c:/folder"; sub edit { print "$File::Find::name"."\n"; } $s = find(\&edit,$dir); open(FILE, ">file.txt"); print FILE $s; close(FILE);
but when I run the code above , I don't get anything at file.txt
what is the wrong in my code , and what shall I do to get the finding result in my file.txt?
My Regard.

Replies are listed 'Best First'.
Re: writing the finding result into a file.txt
by nemesdani (Friar) on Mar 28, 2012 at 09:19 UTC
    The $s contains the return value of the find, not what you found. If you let the sub edit do the writeout, I think it will do what you expect.
    Also use strict :).
Re: writing the finding result into a file.txt
by clueless newbie (Curate) on Mar 28, 2012 at 11:29 UTC
    use File::Find; use strict; use warnings; my $dir = "c:/temp"; sub edit { print FILE "$File::Find::name"."\n"; } open(FILE, ">file.txt"); find(\&edit,$dir); close(FILE);
    added my to $dir = "c:/temp";
      I tried to use strict but it failed , and I tried to use the code above for(clueless newbie)
      but it didn't work :(
      what is the wrong , please help me :)
        I'm assuming you're on Windows. Perhaps you don't have a "C:\temp"? (Be careful to either use "C:/temp" note the use of double quotes and the forward slash or to use 'C:\temp' single quote and backslash. If you double quote and backslash you'll have to escape the backslash i.e. "c:\\temp". Also: add a my to the $dir as in
        my $dir='c:\temp';
Re: writing the finding result into a file.txt
by The Mass (Initiate) on Mar 28, 2012 at 18:22 UTC
    thanks bro , it is work done
    but I have a small question :
    why when we add a my to $dir , it is work done ??
    and without it , it doesn't work?
    please answer my small question .
    thanks again for your helping :)