hey thanks!
didn't realize they have downloadable codes.
Downloaded them and will be going over them.
I still am however looking for ways to optimize that code (but I am sure point of that book wasn't about optimizing
UPDATE: can someone please explain to me what wanted=> sub is all about?
I thought wanted() was a simple sub? so I am not sure what wanted=> sub is doing in the code
{
find
{
wanted => sub
{
## code....
},
no_chdir => 1,
}, $incl_dir;
}
| [reply] [d/l] |
sub { some code } generates an anonymous subroutine and returns a reference to it.
So:
find( { wanted => sub { blah blah } }, @dirs );
is the same as
sub foo { blah blah }
find( { wanted => \&foo }, @dirs );
but without declaring a separate, named subroutine.
Update: fixed to pass hashref and array
| [reply] [d/l] [select] |
I understand now about the anonymous subroutine.
I went over both anonymous sub and also the cpan's doc on this at http://search.cpan.org/~rgarcia/perl-5.10.0/lib/File/Find.pm
However, I still do not understand ..
Doc says that find(\&wanted, @directories);, find function will do a depth search in @directories and for each file/dir found, it will call a anonymous sub wanted
couple questions
1)why is there a curly brace for find at below? find{}, instead of bracket??
All the example I been looking at has find()
2)why are they omitting the second parameter ? list of directories?
Or shouldn't the whole thing be written as
find(
sub {
##code here;
},
@INC,
);
instead of how it's written in the book?
find{ wanted => sub { ##### code ;}, no_chdir => 1,}, $incl_dir;
| [reply] [d/l] [select] |
++ysth is correct about named vs anonymous subroutines. Another confusing issue is that the first argument to find() can be either a code reference or a hash reference.
In the simpler case, where you have no special options to set, you just make the first argument a *code* reference; a reference to the "callback" sub. The sub is always named "wanted" in the documentation, but it can have any name, or be a anonymous sub.
find(
\&some_subroutine,
@directories_to_search,
);
If you need to set a special option in find(), like 'no_chdir', then you pass a *hash* reference as the first argument. The code reference (that you would have used as the first argument in the simpler case) becomes the 'wanted' option.
find(
{
wanted => \&some_subroutine,
other_option => 'other_option_value',
# ...
},
@directories_to_search,
);
| [reply] [d/l] [select] |