hakkr,

The first example works because the lexical @files is visible to the FileName subroutine (since both are defined within the Get_Saves subroutine block). This works because you have the scope correct.

The second example will not work for several reasons: 1, File::Find::find does not return anything; 2, \&FileName(\@files) is not a reference to a subroutine (only \&FileName is); and three, the @files in Get_Saves is a different beastie than the @files in FileName (the first is a lexical and the second is a global.

To use File::Find with the two seperate functions, you would need to use either global @files or a lexical @files defined outside of Get_Saves and FileName:

#!/usr/bin/perl use strict; use File::Find; my @files; # could leave this out and just let FileName # auto-vificate global @files Get_Saves(); sub Get_Saves { find( \&FileName, "/usr/local/apache/htdocs/service/" ); } sub FileName { unless( ($_ eq ".") or ($_ eq "temp.svd")) push( @files, $_ ); # This @files is either the lexical declared above # or an auto-vifified global if nothing was declared # above. }

update I really like goldclaw's Re: Sub in a Sub & Recursion but you would need to remove the "@files=" from the find since File::Find::find always returns nothing. Just do this:

#where to add stuff. find(FileName(\@files),"/usr/local/apache/htdocs/service/"); return \@files, $#files+1;

-derby


In reply to Re: Sub in a Sub & Recursion by derby
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.