in reply to Re: Sub in a Sub & Recursion
in thread Sub in a Sub & Recursion

Thanks,
I've used the closure solution with derby's fix which has given me an insight into a practical use for closures. 'our' worked as well but I've not seen it used here much before and my instinct says to stay away from globalish things.
Update Cheers for the recursion tips anon monk reads almost like a tutorial. Recursion is a way of thinking I'm determined to bend my head around

Replies are listed 'Best First'.
Re(3): Sub in a Sub & Recursion
by dmmiller2k (Chaplain) on Jan 22, 2002 at 08:43 UTC

    It seems to me, that if you want to use a closure for such a thing, you'll want to use an anonymous sub instead of your FileName sub. Then the @files array will be shared as you expect it to be (e.g., each time Get_Saves is called, $sub will be bound to the new instance of @files).

    sub Get_Saves { my @files; my $sub = sub { unless (($_ eq "." )or ($_ eq "temp.svd")){ push @files, $_; } }; find ($sub,"/usr/local/apache/htdocs/service/"); return \@files, $#files+1; }

    As to how to make two separate subs, derby seems to have already answered that. You'd "need to use either global @files or a lexical @files defined outside of Get_Saves and FileName".

    Hope this helps

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime