in reply to Recursive sub

First - if you're doing this for a learning exercise, that's fine. But, using File::Find is NOT "stealing code". It is using well-tested and well-written solutions to common problems. I hope you wouldn't think you'd have to rewrite CGI, DBI, or XML::Parser because you'd be "stealing code".

That said, you have a few "issues" with your code. Try the following:

use File::Spec; my $dir = "C:\"; sub recurseDir2 { foreach (glob shift) { if (-d) { print "Directory: '$_'\n" ; recurseDir2(File::Spec->catfile($_, '*')); next; } if (-f) { print "File: '$_'\n"; } } } recurseDir2($dir)

I wasn't able to reproduce your problem, but I think I know where it might have come from. You're on a Windows system, but using the Unix directory syntax. This is why modules exist, to make sure you don't get screwed up. File::Spec will make sure you use the right directory separator for your system. This isn't "stealing code" - this is intelligent re-use of well-thought out solutions.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Recursive sub
by pen (Acolyte) on Sep 23, 2003 at 19:27 UTC
    Thanks dragonchild, I am aware that I am not stealing code. I was just making the point that I don't want to just cut and paste code that I don't understand. For me to learn Perl I need to be able to write my own code so that I get a full understanding.

    I hadn't seen the File::Spec module, I will read up on it.

    In your code you are short forming

     foreach (glob shift)

    I assume by not supplying the array. Is this the case?

    This is one area that I am having difficutly in reading other peoples code at this point, Perl makes allot of assumptions that I am not aware of yet, or just don't remember at this point.

    Paul Neale

      There are not many that I can help because of my relative new-ness to perl! But you seem willing to put forth the effort, and I can answer some of your questions. Well, I would like to suggest you pick up a copy of Learning Perl 3rd Ed Written by our very own Merlyn. Trust me, you will learn a lot from it.

      In the line foreach (glob shift) Perl is using @_ as it's default.

      LR

      Whip me, Beat me, Make me use Y-ModemG.