Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: File::Find preprocess

by pvaldes (Chaplain)
on Jan 08, 2018 at 16:33 UTC ( [id://1206912] : note . print w/replies, xml ) Need Help??

in reply to File::Find preprocess

Shouldn't exit, just close the function "before"?.

If I remove all "return" and "exit" the program process the parent dir, but not the subdirs (prints: file! hi file! bye)

Replies are listed 'Best First'.
Re^2: File::Find preprocess
by stevieb (Canon) on Jan 08, 2018 at 16:58 UTC

    No. The problem here is that you aren't completely reading documentation.

    exit exits an entire process stack (ie. your program). return does leave the subroutine.

    This, combined with the preprocess misconception is what's causing your issues.

      Hum, I see. I was expecting an equivalent to the "previously" condition in a common-lisp loop structure. A chunk of code that happens only once before the main body of the function starts iterating. I understand that if I need to do something within each subdirectory (check if something happens inside each subdir first), I would need then to traverse the tree two times using two different find functions. One for directories only, and a second for all files. Right? can't be done in one take?

        I'm a bit unclear exactly what you're asking, but if you want to do something for files and something different for directories:

        sub wanted { if (-d){ print "directory\n"; } if (-f){ print "file\n"; } }
Re^2: File::Find preprocess
by pvaldes (Chaplain) on Jan 08, 2018 at 16:42 UTC

    And more strange, If I change the function wanted like this:

    sub when { print $File::Find::name," "; }

    I obtain: ". hi ./1 bye", even if the file "1" does not exist

    Updated: I mean file::find, not find::file

      Per the documentation, preprocess receives a list (of entries in the current directory), and is expected to return a list (of entries). The purpose for this functionality is to provide you the ability to prune, filter, reorganize etc the list of entries as you sweep over them before the wanted function is called (it receives the list returned from preprocess).

      The most basic test you can do is just immediately return the list the begin function receives:

      sub begin { my @entries = @_; print "begin\n"; return @entries; }

      The reason you're getting "1", is because that's what a successful print statement returns, and in Perl, if you don't have an explicit return(), the result of the last expression evaluated is returned (in your case, the result of the print, meaning "1").

      my $x = print "hi\n"; print "$x\n"; __END__ hi 1