School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,476 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,743 people online right now. Registration is fast and FREE... Join Now!




Perl Variables

 

Perl Variables

n00bc0der

28 May, 2009 - 06:05 AM
Post #1

D.I.C Head
**

Joined: 27 Nov, 2008
Posts: 62



Thanked: 3 times
My Contributions
Hey guys,

So I'm writing some code in perl where I want to use a sub-routine that recursively calls it self to find a list of directories & subdirectories from a given directory. I want to collect all of these directories into one large array. The only language that I have recursive practice in is java, and then I could just set it to the an instance variable and continue to populate the array as the code went through.

My question is: How can I recursively call a sub-routine to populate an array?

Here's the code I have so far:

CODE
@dirlist = ();

#Used to detect all directories in a given original directory
sub getfulldirectorylist {
    my $dirname = @_;
    my @templist = `cd $dirname; ls -d */`;
    if (@templist) {    
        foreach $templist (@templist) {
            push(@dirlist, $templist);
            foreach $dirlist (@dirlist) {
                getfulldirectorylist($dirlist);
            }
        }
    }
}


Thanks Guys!

User is offlineProfile CardPM
+Quote Post


girasquid

RE: Perl Variables

28 May, 2009 - 07:23 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,628



Thanked: 53 times
Dream Kudos: 825
My Contributions
I would say that the code you have will populate the array with all of your directories.

However, you should take a look at the File::Find module - as it will do all this for you.
User is offlineProfile CardPM
+Quote Post

n00bc0der

RE: Perl Variables

28 May, 2009 - 08:19 AM
Post #3

D.I.C Head
**

Joined: 27 Nov, 2008
Posts: 62



Thanked: 3 times
My Contributions
QUOTE
However, you should take a look at the File::Find module - as it will do all this for you


I'm unable to use any of these modules because of the product I'm producing. It's a complicated/confidential thing.

Thanks.
User is offlineProfile CardPM
+Quote Post

girasquid

RE: Perl Variables

28 May, 2009 - 08:41 AM
Post #4

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,628



Thanked: 53 times
Dream Kudos: 825
My Contributions
As far as I'm aware, File::Find exists within the Perl core - using it is no different than using Perl itself(or strict, warnings, or diagnostics).
User is offlineProfile CardPM
+Quote Post

chorny_cpan

RE: Perl Variables

28 May, 2009 - 07:57 PM
Post #5

New D.I.C Head
Group Icon

Joined: 13 May, 2009
Posts: 35


Dream Kudos: 25
My Contributions
QUOTE(n00bc0der @ 28 May, 2009 - 08:19 AM) *

I'm unable to use any of these modules because of the product I'm producing. It's a complicated/confidential thing.



See Of course you can use CPAN.
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Perl Variables

29 May, 2009 - 02:41 AM
Post #6

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(n00bc0der @ 28 May, 2009 - 02:05 PM) *

So I'm writing some code in perl where I want to use a sub-routine that recursively calls it self to find a list of directories & subdirectories from a given directory. I want to collect all of these directories into one large array.

My question is: How can I recursively call a sub-routine to populate an array?

By calling it from within itself. You don't have to do anything special in Perl to enable recursion.

QUOTE(n00bc0der @ 28 May, 2009 - 02:05 PM) *

Here's the code I have so far:

CODE
@dirlist = ();

#Used to detect all directories in a given original directory
sub getfulldirectorylist {
    my $dirname = @_;
    my @templist = `cd $dirname; ls -d */`;
    if (@templist) {    
        foreach $templist (@templist) {
            push(@dirlist, $templist);
            foreach $dirlist (@dirlist) {
                getfulldirectorylist($dirlist);
            }
        }
    }
}


It looks to me like you've set up a fairly complex infinite loop here. When you find the second directory, the way your loops are set up will cause them to run getfulldirectorylist again on the first directory, which will call gfdl on the first directory again, etc. (I haven't run it to verify this, but pushing each directory onto @dirlist, then calling recursively for every entry in @dirlist (including those that have already been processed) should have that effect.)

Also, even if it were to terminate, you haven't told it what to return, so it would just return the value of the last statement processed, which is unlikely to be the result you're looking for.

Seriously, if you're going to go out to the shell for this anyhow, the easiest way to accomplish it would be to have ls handle the recursion itself:
CODE

sub getfulldirectorylist {
  my $dir = shift;
  return `cd $dir; ls -Rp | grep /\$`;
}


QUOTE(n00bc0der @ 28 May, 2009 - 04:19 PM) *

QUOTE
However, you should take a look at the File::Find module - as it will do all this for you


I'm unable to use any of these modules because of the product I'm producing. It's a complicated/confidential thing.

1) Everything in CPAN is under some open source license or other and the vast majority (including File::Find) is licensed under the same terms as Perl itself. If you can use Perl, then using CPAN modules under the same license would raise no additional licensing or confidentiality issues. Even if it were one of the few CPAN modules licensed only under GPL, there would be no licensing or confidentiality issues raised unless you distributed the code in a compiled binary form; as the default for Perl programs is to distribute in source form, GPL would still be unlikely to cause any actual issues.

2) girasquid is correct. File::Find is a core module: http://perldoc.perl.org/index-modules-F.html If you have Perl installed, then you already have File::Find installed also, unless someone made a deliberate effort to build a Perl distribution without it.
User is offlineProfile CardPM
+Quote Post

n00bc0der

RE: Perl Variables

29 May, 2009 - 06:04 AM
Post #7

D.I.C Head
**

Joined: 27 Nov, 2008
Posts: 62



Thanked: 3 times
My Contributions
Alright so I managed to change the code to this:

CODE
sub getfulldirectorylist {
    my @dirlist = ();
    my $dirtop = @_[0];
    @dirlist = `find $dirtop -type d`;
    return @dirlist;
}


It works great.

Now I'm faced with a new problem, how to compare the text of a "baseline file" with other systems. Any ideas on this?
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Perl Variables

30 May, 2009 - 12:56 AM
Post #8

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(n00bc0der @ 29 May, 2009 - 02:04 PM) *

Now I'm faced with a new problem, how to compare the text of a "baseline file" with other systems. Any ideas on this?

rsync? You can use -n to tell it to only compare the files without synchronizing them and exporting RSYNC_RSH=ssh in your environment will tunnel the rsync session through ssh, thus avoiding the need to set up a separate service (with questionable security) on the remote machine.

Another option would be to use some form of revision control software, such as cvs, svn, or git. All of these three include diff functionality which will show you how the current local version of each file differs from the repository's version.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:30AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month