in reply to Re: Recursive function in perl
in thread Recursive function in perl

Sorry for the confusion....
Inside the main, there will be some filenames.....

I want to get the filenames first....Then open each file...Inside each file there will be some other filename....then i will take these file name and open those filenames...So it will continue until the depth we are specifying....

When the depth is achieved, it should print the file contents....from the main file to the depth

Replies are listed 'Best First'.
Re^3: Recursive function in perl
by 1Nf3 (Pilgrim) on Jun 19, 2009 at 12:35 UTC

    Thank you for the clarification. I hope this gets you started.

    #!/usr/bin/perl -w use strict; my $initial_file = 'Main'; my $ultimate_level = 2; sub jak_daxter_recursor_legacy { my ($file, $level) = @_; open (FH, $file) or die "Cannot open $file: $!"; my $contents = <FH>; my @filestoprocess = split (/,/, $contents); for my $file (@filestoprocess) { print "$file\n"; jak_daxter_recursor_legacy ($file, $level+1) unless ($level == $ultimate_level); } } jak_daxter_recursor_legacy ($initial_file, 1);

    I tested it using three files:
    Main, containing 'sub1,sub2'
    sub1, containing 'sub12,sub13,sub17'
    sub2, containing 'sub21,sub22'

    The output is as follows:

    sub1 sub12 sub13 sub17 sub2 sub21 sub22

    Hope it helps. If you have any questions, please ask.

    Regards,
    Luke