7 Replies - 1588 Views - Last Post: 30 May 2009 - 01:56 AM

#1 n00bc0der   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 27-November 08

Perl Variables

Posted 28 May 2009 - 07:05 AM

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:

@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!

Is This A Good Question/Topic? 0
  • +

Replies To: Perl Variables

#2 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Perl Variables

Posted 28 May 2009 - 08:23 AM

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.
Was This Post Helpful? 0
  • +
  • -

#3 n00bc0der   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 27-November 08

Re: Perl Variables

Posted 28 May 2009 - 09:19 AM

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.
Was This Post Helpful? 0
  • +
  • -

#4 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Perl Variables

Posted 28 May 2009 - 09:41 AM

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).
Was This Post Helpful? 0
  • +
  • -

#5 chorny_cpan   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 13-May 09

Re: Perl Variables

Posted 28 May 2009 - 08:57 PM

View Postn00bc0der, on 28 May, 2009 - 08:19 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#6 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl Variables

Posted 29 May 2009 - 03:41 AM

View Postn00bc0der, on 28 May, 2009 - 02:05 PM, said:

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.

View Postn00bc0der, on 28 May, 2009 - 02:05 PM, said:

Here's the code I have so far:

@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:
sub getfulldirectorylist {
  my $dir = shift;
  return `cd $dir; ls -Rp | grep /\$`;
} 



View Postn00bc0der, on 28 May, 2009 - 04:19 PM, said:

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....-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.
Was This Post Helpful? 0
  • +
  • -

#7 n00bc0der   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 62
  • Joined: 27-November 08

Re: Perl Variables

Posted 29 May 2009 - 07:04 AM

Alright so I managed to change the code to this:

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?
Was This Post Helpful? 0
  • +
  • -

#8 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl Variables

Posted 30 May 2009 - 01:56 AM

View Postn00bc0der, on 29 May, 2009 - 02:04 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1