Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: while following perl hack book

by convenientstore (Pilgrim)
on Feb 04, 2008 at 04:35 UTC ( [id://665903]=note: print w/replies, xml ) Need Help??


in reply to Re: while following perl hack book
in thread while following perl hack book

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; }

Replies are listed 'Best First'.
Re^3: while following perl hack book
by ysth (Canon) on Feb 04, 2008 at 08:59 UTC
    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

      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;
        Look at the synopsis in File::Find again. Passing a hashref instead of a coderef (i.e. { wanted => ..., }) allows (but doesn't require) you to specify other options, like no_chdir or follow.

        1. The hack author just omitted the parens. When find is predeclared (or imported in this case), these two lines are identical:
          find sub { ... }, $dir_name ; find( sub { ... }, $dir_name );
        2. I thought that as well, but when I tried to refactor to the more efficient form (passing all directories at once), I noticed that the code in the &wanted sub needs to know the "root" directory name: $file =~ s{^\Q$incl_dir/\E}{ };. Passing all the dirs at once removes the ability to see $incl_dir.

Re^3: while following perl hack book
by Util (Priest) on Feb 05, 2008 at 02:33 UTC

    ++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, );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://665903]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-18 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found