[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Please help me with FSpIterateDirectory



Frederick Bruckman <fb@enteract.com>:
>Jim Burton <jim@burtcom.com> asks:
>>I recently discovered the procedure in Mac::MoreFiles called
>>FSpIterateDirectory, which is supposed to walk through a specified
>>directory and call a specified subroutine for each file found. I've tried
>>the following code:
>>-----
>>#!/usr/bin/perl
>>
>>#walker!
>>
>>use Mac::MoreFiles;
>>FSpIterateDirectory (":",0,DoThis,dummy);
>>print "\nFinished\n\n";
>>
>>sub DoThis {
>> print "Information returned: @_\n";
>>	}
>>-----
>>And it seems to return information about only the first file in the
>>directory. What have I done wrong?

>Your subroutine never returns! Note that it never prints the "Finished,"
>either. Add
>
>	return 0;
>
>to the end of the subroutine, and it will do what you want.

The "return 0" statement is indeed necessary, but not quite for the reason
you claim. The reason is related to three facts:

 - If the subroutine in FSpIterateDirectory returns 1, the iteration is 
   terminated. This is a feature, but not documented anywhere in the 
   documentation (and, as Tim Dierks pointed out, the documentation is wrong
   about one other point).
 - Subroutines without return statements return the value of the last statement
   in the subroutine. This is documented but easily forgotten.
 - "Print" under normal circumstances returns a nonzero value, like most Perl
   operations with no obvious return value.

Matthias

-----
Matthias Neeracher   <neeri@iis.ee.ethz.ch>   http://www.iis.ee.ethz.ch/~neeri
   "I'm set free to find a new illusion" -- Velvet Underground

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch