> Hi > > I am a newbie to Perl , I was reading through one of the beginner level > books on perl. I did not understand the concept of "Callbacks" and i > have the following questions on it: > > 1. What are they ? > > 2. Why do we need them ? > > 3. What useful purpose do they achieve ? > > I was reading the following code to understand it but could not comprehend. > > *Code:* > > #!/usr/bin/perl > use warnings; > use strict; > use File::Find; > find ( \&callback, "/"); > > sub callback { > print $File::Find::name, "\n"; > } > > *End Of Code:* > > Thanks > Jatin
Hi Jatin, A callback is a reference to a subroutine. This reference when passed around, allows other code to invoke it. File::Find's find() method accepts a subroutine reference as the first argument and a path in the filesystem as the second argument. find() traverses recursively in '/' and calls your code reference (callback()) for each file/directory it finds. Thus, your subroutine is able to get each item in the path as soon as they are encountered by File::Find's find(). If find() was not implemented to handle callbacks, the possible way to return encountered file/directory names will be as an array or hash of file/directory names after it has traversed and exhausted all possible file/directory names within the path. Regards, Alan Haggai Alavi. -- The difference makes the difference. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/