Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Return values, Closure Vars from File::Find &wanted subs?

by Subliminal Kid (Initiate)
on Nov 16, 2005 at 16:54 UTC ( [id://509101]=perlquestion: print w/replies, xml ) Need Help??

Subliminal Kid has asked for the wisdom of the Perl Monks concerning the following question:

How do I get the list of files I create with File:Find's wanted sub out of that subroutine and returned to my main program?
use File::Find; find(\&wanted, shift(@ARGV)); # I would like the @dirlist here # but I can't get it returned for the life of me. exit; sub wanted { return unless -d $_; my @dirlist; push(@dirlist,$File::Find::name); return(@dirlist); }
Someone said to use Closure Vars and I read "Subroutine References and Closures" from Advanced Perl Programming but I'm not getting it. HELP Cheers, James
Sorry, Lets try the long version since I seem to have miss queued the answers (My mistake)
Sorry, I'm obviously not making myself clear here.
I don't want to use a Global variable.

I have a program in the form:
use strict; use File::Find; sub main; my @dirlist; main(); exit; sub main { findit(); dostuff($_) for @dirlist; } sub findit { find(\&wanted, shift(@ARGV)); } sub wanted { return unless -d $_; push(@dirlist,$File::Find::name); } sub dostuff { print "$_\n" }
and I want to do it without the global my @dirlist at the top.

I have dumped about 800 odd lines of someone's code & 60 odd subs from this example so massive rewrites aren't the order of the day but I do need to take out all these bloody global vars because they are making my life a real headache.
I'm not sure I understand why I can't just return the array from &wanted or at the very least declare it in findit and have wanted inherit it but <shrugs>.

Replies are listed 'Best First'.
Re: Return values, Closure Vars from File::Find &wanted subs?
by ikegami (Patriarch) on Nov 16, 2005 at 17:03 UTC

    Increase the scope of @dirlist:

    use File::Find; my @dirlist; sub wanted { return unless -d $_; push(@dirlist, $File::Find::name); } find(\&wanted, shift(@ARGV)); # Use @dirlist here

    Closures come in useful if you'd wish to avoid using a global:

    use File::Find; sub find_dirs { my @dirlist; find(sub { return unless -d $_; push(@dirlist, $File::Find::name); }, @_); return @dirlist; } { # Not a global. my @dirlist = find_dirs(shift(@ARGV)); # Use @dirlist here }

    Yet another way. This one uses dynamically scoped variables:

    use File::Find; sub wanted { return unless -d $_; push(@File::Find::matches, $File::Find::name); } sub find_it { local *File::Find::matches; find(@_); return @File::Find::matches; } { # Not a global. my @dirlist = find_it(\&wanted, shift(@ARGV)); # Use @dirlist here }

    Update: Added 2nd snippet.
    Update: Added 3rd snippet.

Re: Return values, Closure Vars from File::Find &wanted subs?
by Roy Johnson (Monsignor) on Nov 16, 2005 at 17:06 UTC
    If you move the declaration of @dirlist outside the wanted sub, you will be pushing to the same array every time. Enclosing the variable declaration with the sub in a block limits the scope of @dirlist, creating a closure.
    { my @dirlist; sub wanted { return unless -d $_; push(@dirlist,$File::Find::name); return(@dirlist); } }
    As xdg notes in reply, this is not the closure you actually want, though. Your wanted sub shouldn't be returning a list.

    Caution: Contents may have been coded under pressure.

      That's really the same thing as just having the file be the enclosing scope. It would be different if this were for a subroutine that returned the list.

      sub get_dirlist { my $dir_to_scan = shift; my @dirlist; my $wanted = sub { return unless -d $_; push(@dirlist,$File::Find::name); } find( $wanted, $dir_to_scan ); return @dirlist; }

      The anonymous subroutine attached to $wanted is compiled only once, but gets a fresh copy of @dirlist for each invokation. Contrast that to this, which would continue to add to @dirlist each time the get_dirlist function was called and return a longer and longer array each time:

      my @dirlist; sub wanted { return unless -d $_; push(@dirlist,$File::Find::name); } sub get_dirlist { my $dir_to_scan = shift; find( \&wanted, $dir_to_scan ); return @dirlist; }

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Return values, Closure Vars from File::Find &wanted subs?
by xdg (Monsignor) on Nov 16, 2005 at 17:07 UTC
    use File::Find; # set up closure here my @dirlist; sub wanted { return unless -d $_; push(@dirlist,$File::Find::name); } # call the sub to fill @dirlist find(\&wanted, shift(@ARGV)); # use results here print "$_\n" for @dirlist;

    @dirlist is visible within the sub, and the push will work as normal. A "closure" essentially boils down to using a lexical ("my") variable from outside a subroutine inside a subroutine. See the Closure on Closures tutorial.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Return values, Closure Vars from File::Find &wanted subs?
by ikegami (Patriarch) on Nov 16, 2005 at 17:45 UTC
    In response to your updated question
    use strict; use warnings; use File::Find; main(); exit; sub main { { dostuff($_) foreach findit(shift(@ARGV)); } sub findit { my @dirlist; find(sub { return unless -d $_; push(@dirlist, $File::Find::name); }, @_); return @dirlist; } sub dostuff { my ($dir) = @_; print "$dir\n" }

    This is nothing more than applying my original node.

Re: Return values, Closure Vars from File::Find &wanted subs?
by radiantmatrix (Parson) on Nov 16, 2005 at 19:25 UTC

    I don't think you get how the 'wanted' function works. It is run once for each file and directory in the tree you're searching.

    If you don't want a global array to store these results, you have two choices: you can either make the wanted function do the work on each file, or you can change the structure of your program. The former might look like:

    sub main { findit(); # dostuff($_) for @dirlist; } sub findit { find(\&wanted, shift(@ARGV)); } sub wanted { return unless -d $_; # push(@dirlist,$File::Find::name); dostuff($File::Find::name); } sub dostuff { print "$_\n" }

    I commented out the lines in your code, so you can more easily see the change.

    The latter solution might look something like this:

    sub main { my @dirlist; find( sub { return unless -d $_; push(@dirlist,$File::Find::name); }, (shift @ARGV) ); dostuff($_) for @dirlist; } sub dostuff { print "$_\n"; }

    I highly recommend the first approach -- it seems silly, in most cases, to build a list of files and then act on it when you can act on each file as you find it.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
Sorry, Lets try the long version.
by Subliminal Kid (Initiate) on Nov 16, 2005 at 17:33 UTC
    Sorry, I'm obviously not making myself clear here.
    I don't want to use a Global variable.

    I have a program in the form:
    use strict; use File::Find; sub main; my @dirlist; main(); exit; sub main { findit(); dostuff($_) for @dirlist; } sub findit { find(\&wanted, shift(@ARGV)); } sub wanted { return unless -d $_; push(@dirlist,$File::Find::name); } sub dostuff { print "$_\n" }
    and I want to do it without the global my @dirlist at the top.

    I have dumped about 800 odd lines of someone's code & 60 odd subs from this example so massive rewrites aren't the order of the day but I do need to take out all these bloody global vars because they are making my life a real headache.
    I'm not sure I understand why I can't just return the array from &wanted or at the very least declare it in findit and have wanted inherit it but <shrugs>.

      Have you considered passing and returning data to/from these subs?

      use strict; use File::Find; main(); exit; sub main { dostuff($_) for findit( @ARGV ); } sub findit { my( $root ) = @_; my @dirlist; find( sub { -d $_ or push @dirlist, $File::Find::name; }, $root ); @dirlist } sub dostuff { print "$_\n" }

      It should be noted that the above uses a closure, which is one way to "eliminate" global variables without unduly restricting access to them.

      We're building the house of the future together.
      You don't need to separate your variable declarations from your sub declarations. You can declare @dirlist just before the first subroutine that uses it. The declaration line will never be executed (because you exit before it gets to the declaration, so don't try to initialize it — you'd need to do that in a BEGIN block) but will be visible to the subs below it because the declaration is handled at compile time.

      For tighter scoping, you can enclose it in a block with the subs that use it (main and wanted in your example code).

      ikegami's solution shows you how to do multiple finds that don't all write to the same array. That is one of the main things closures are useful for.


      Caution: Contents may have been coded under pressure.
Re: Return values, Closure Vars from File::Find &wanted subs?
by adrianh (Chancellor) on Nov 17, 2005 at 16:25 UTC

    Try File::Find::Rule. Your example would translate as something like (untested):

    use File::Find::Rule; dostuff($_) for File::Find::Rule->file->in( @ARGV );

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found