Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: while following perl hack book

by Util (Priest)
on Feb 04, 2008 at 04:28 UTC ( [id://665902]=note: print w/replies, xml ) Need Help??


in reply to while following perl hack book

The problem with the original, as you posted it, is that all the backslashes have been doubled. Change all occurances of '\\' into '\', and it works fine.

The downloadable code from O'Reilly does not have the doubled backslashes, (and neither does the copy of the page at Google Books), but it is missing a trailing semi-colon from the $LIST_DIR line.
In the tarball, the path is perl_hacks_examples/productivity_hacks/autocomplete_perl_identifiers_in_vim/find_pm_files.pl.

Replies are listed 'Best First'.
Re^2: while following perl hack book
by convenientstore (Pilgrim) on Feb 04, 2008 at 04:35 UTC
    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; }
      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;

      ++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://665902]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found