Heres a good use for closeures. The way File::Find works there are no good way to pass extra arguments into the wanted subroutine without using closeures. So for your example you could do something like this:
sub Get_Saves { my @files; #Just a small mod here. Call FileName since it now #returns a suitable closeure, and pass in the #reference to @files so that FileName knows #where to add stuff. @files=find(FileName(\@files),"/usr/local/apache/htdocs/service +/"); return \@files, $#files+1; }##END Get_Saves sub FileName { my $files_ref=shift; #Use reference to array return sub { unless (($_ eq "." )or ($_ eq "temp.svd")){ push @{$files_ref}, $_; } #removed this since returning anything from the #wanted function does not really make any sence(See #File::Find) #return @files } }

A few years back a colleuge of mine was horrified of the File::Find module, since you basically had to use "global" variables to be able to pass arguments in to it. I was sure that couldnt be so, but I didnt know/understand closeures at the time. He ended up writing his own find routine, and I twisted my brain for days on how to do this with File::Find.

The solution came to me months later when I learned about closeures. Unfortunately I had quit the company by then, so I couldnt tell him how he had wasted his time reimplementing File::Find. Anyway, I guess the moral is something like:Whenever File::Find doesnt seem to do all you want, start reading about closeures again.

GoldClaw


In reply to Re: Sub in a Sub & Recursion by goldclaw
in thread Sub in a Sub & Recursion by hakkr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.