Recursive function

2004-11-16 Thread Eaton Elyahu Doron
Hi all,

  Why is the following won't work recursively ?

  The purpose is simple, search directory inside $depth levels for
directories. But since the stop recursive is when depth is "0", after
the search starts the first directory it stops when it reaches the level
in that first directory and doesn't continue to the next one.

  I though I declared it as "my" so any callback restarting it !

Your answers will be appreciated,
Eli.


== Start of code 
#!/usr/bin/perl

my (@dirs,$d,$depth) ;

$depth = 2 ;
@dirs = &find_dirs ($ARGV[0],$depth) ;

print "\n\nOutput is ...\n" ;
foreach $d (@dirs)
{
print " $d\n" ;
}

1 ;

sub find_dirs
{
my ($dir,$depth) = @_ ;
my (@dirs,$d,$exit_status) ;
my ($dir_ls) = "dir /AD" ;

if ($depth > 0)
{
$depth-- ;

chdir ($dir) ;
$exit_status = `dir` ;
print "\nCurrently in -
>\n$exit_status\n==\nDepth ($depth)\n" ;
open (DIR_LS,"$dir_ls |") ;
while ()
{
next if /^\s+/ ;
chmop;
$d = $_ ;
$d =~ s/.*\s+// ;
$d =~ s/\s//g ;
next if ($d =~ /\.\.?/) ;
next if ($d =~ /^Export/) ;
print "Collecting  $d\n" ;
push (@dirs,$d) ;
&find_dirs ($d,$depth) ;
}
close DIR_LS ;
}
return @dirs ;
}

=== End of code



Re: Recursive function

2004-11-17 Thread Dave Gray
>   The purpose is simple, search directory inside $depth levels for
> directories.

No need to reinvent the wheel:
 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Recursive function

2004-11-21 Thread Eaton Elyahu Doron

Yes, thanks a lot, I know I can use the find.pm package. But, I wantted
to learn what did I do wrong about my code.

I'd be mostly thankful for you inputs,
Eli.


== Start of code
#!/usr/bin/perl

my (@dirs,$d,$depth) ;

$depth = 2 ;
@dirs = &find_dirs ($ARGV[0],$depth) ;

print "\n\nOutput is ...\n" ;
foreach $d (@dirs)
{
print " $d\n" ;
}

1 ;

sub find_dirs
{
my ($dir,$depth) = @_ ;
my (@dirs,$d,$exit_status) ;
my ($dir_ls) = "dir /AD" ;

if ($depth > 0)
{
$depth-- ;

chdir ($dir) ;
$exit_status = `dir` ;
print "\nCurrently in -
>\n$exit_status\n==\nDepth ($depth)\n" ;
open (DIR_LS,"$dir_ls |") ;
while ()
{
next if /^\s+/ ;
chmop;
$d = $_ ;
$d =~ s/.*\s+// ;
$d =~ s/\s//g ;
next if ($d =~ /\.\.?/) ;
next if ($d =~ /^Export/) ;
print "Collecting  $d\n" ;
push (@dirs,$d) ;
&find_dirs ($d,$depth) ;
}
close DIR_LS ;
}
return @dirs ;
}

=== End of code 

-Original Message-
From: Dave Gray [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, November 17, 2004 7:39 PM
To: Eaton Elyahu Doron
Cc: [EMAIL PROTECTED]
Subject: Re: Recursive function

>   The purpose is simple, search directory inside $depth levels for 
> directories.

No need to reinvent the wheel:
 <http://search.cpan.org/~nwclark/perl-5.8.5/lib/File/Find.pm>
__
  This email message has been scanned by PineApp Mail-Secure and has
been found clean.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: Recursive function

2004-11-23 Thread Eaton Elyahu Doron

I sent it again, since I didn't got a reply that satisfy me.

I know I can use the "find.pm". But, I'd like to know why the recoursive
I did is wrong.

Thanks,
Eli.

-Original Message-
From: Eaton Elyahu Doron [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, November 16, 2004 9:44 PM
To: [EMAIL PROTECTED]
Subject: Recursive function

Hi all,

  Why is the following won't work recursively ?

  The purpose is simple, search directory inside $depth levels for
directories. But since the stop recursive is when depth is "0", after
the search starts the first directory it stops when it reaches the level
in that first directory and doesn't continue to the next one.

  I though I declared it as "my" so any callback restarting it !

Your answers will be appreciated,
Eli.


== Start of code
#!/usr/bin/perl

my (@dirs,$d,$depth) ;

$depth = 2 ;
@dirs = &find_dirs ($ARGV[0],$depth) ;

print "\n\nOutput is ...\n" ;
foreach $d (@dirs)
{
print " $d\n" ;
}

1 ;

sub find_dirs
{
my ($dir,$depth) = @_ ;
my (@dirs,$d,$exit_status) ;
my ($dir_ls) = "dir /AD" ;

if ($depth > 0)
{
$depth-- ;

chdir ($dir) ;
$exit_status = `dir` ;
print "\nCurrently in -
>\n$exit_status\n==\nDepth ($depth)\n" ;
open (DIR_LS,"$dir_ls |") ;
while ()
{
next if /^\s+/ ;
chmop;
$d = $_ ;
$d =~ s/.*\s+// ;
$d =~ s/\s//g ;
next if ($d =~ /\.\.?/) ;
next if ($d =~ /^Export/) ;
print "Collecting  $d\n" ;
push (@dirs,$d) ;
&find_dirs ($d,$depth) ;
}
close DIR_LS ;
}
return @dirs ;
}

=== End of code

__
  This email message has been scanned by PineApp Mail-Secure and has
been found clean.




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Recursive function

2004-11-23 Thread Dave Gray
> I know I can use the "find.pm". But, I'd like to know why the recoursive
> I did is wrong.

There are no debug print statements in your code. There are also a lot
of 'next' statements, which always make me suspicious. See comments in
chopped-up code below.

Good luck,
Dave

> == Start of code
[snip]
> @dirs = &find_dirs ($ARGV[0],$depth) ;
[snip]
> sub find_dirs {
[snip]
> my ($dir_ls) = "dir /AD" ;
[snip]
> open (DIR_LS,"$dir_ls |") ;
> while ()
> {
> next if /^\s+/ ;
# insert debug print statement here
# /me tries to imagine what chmop would do
# assume you mean chomp
> chmop;
> $d = $_ ;
# you probably mean .*? here, and possibly \s*
> $d =~ s/.*\s+// ;
> $d =~ s/\s//g ;
# this will skip lines if there is a dot anywhere in the line
# you probably mean /^\.\.?$/
> next if ($d =~ /\.\.?/) ;
> next if ($d =~ /^Export/) ;
# does this ever print?
> print "Collecting  $d\n" ;
> push (@dirs,$d) ;
> &find_dirs ($d,$depth) ;
> }
# debug print should go here, possibly something like print "found $#dirs\n";
> close DIR_LS ;
[snip]
> === End of code

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Spam:RE: Recursive function

2004-11-23 Thread Michael Kraus
G'day...

These lines look wrong (but I may be wrong):

>   $exit_status = `dir` ;
>   print "\nCurrently in -
> >\n$exit_status\n==\nDepth 
> ($depth)\n" ;

Shouldn't that be: $exit_status = `pwd`;

Also, check out the glob function - it'll handle file listings better
than trying to use the shell all the time. (There are also directory
handling functions, see perlfunc
http://www.xav.com/perl/lib/Pod/perlfunc.html )


Regards,
 

Michael S. E. Kraus
Software Developer
Wild Technology Pty Ltd
___
ABN 98 091 470 692
Level 4 Tiara, 306/9 Crystal Street, Waterloo NSW 2017, Australia
Telephone 1300-13-9453 |  Facsimile 1300-88-9453
http://www.wildtechnology.net
 
The information contained in this email message and any attachments may
be confidential information and may also be the subject of client legal
- legal professional privilege. If you are not the intended recipient,
any use, interference with, disclosure or copying of this material is
unauthorised and prohibited.   This email and any attachments are also
subject to copyright.  No part of them may be reproduced, adapted or
transmitted without the written permission of the copyright owner.  If
you have received this email in error, please immediately advise the
sender by return email and delete the message from your system.

 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




error "called to early to check prototype" using recursive function

2005-01-28 Thread S.A. Birl
2 problems, maybe related.

1) My recursive program is generating a "called to early to check
   prototype" error.  Ive Googled for answers but cannot find a solution

2) When this executes, it traverses 8 directories in F:\\Inetpub\\Develop
   then stops and traverses 8 directories in F:\\Inetpub\\Publish

   I cannot seem to figure out why it's giving up so early when there's
   more than 8 directories in each folder.


Thanks
 Birl

Please do not CC me responses to my own posts.
I'll read the responses on the list.





use strict;
use warnings;

my $indent=0;
my $ERRORS=0;

my $OUTPUT="D:\\Logs\\IIS\\unlock_Read-Only_files.log";

sub OpenDirectory($)
{
$indent++;

my $counter=0;
my $Directory=shift;
my $FILE;
my $LongDirectoryName="";



for ($counter=1; $counter < $indent; $counter++) { printf("  "); }

if ( (defined $LongDirectoryName) && ($LongDirectoryName !~ /^$/) )
{
$LongDirectoryName=sprintf("%s\\%s", $LongDirectoryName, 
$Directory);
}
else
{
$LongDirectoryName=$Directory;
}


printf(Loghandle "Opening directory \"%s\":  ", $LongDirectoryName);

if ( opendir(DIRhandle, "$LongDirectoryName") )
{
printf(Loghandle "success\n");

while ( defined ($FILE = readdir(DIRhandle)) )
{
for ($counter=0; $counter < $indent; $counter++) { 
printf(Loghandle "  "); }

if ($FILE =~ /\.\.?$/)
{
printf(Loghandle "skipping \"%s\"\n", $FILE);
}
else
{
if(-f "$LongDirectoryName\\$FILE")
{
#printf(Loghandle "\"%s\" is a 
file.\n", $FILE);
if ( system("attrib -R 
$LongDirectoryName\\$FILE") )
{
for ($counter=0; $counter < 
$indent; $counter++) { printf(Loghandle "  "); }
printf(Loghandle "FAILED: file 
attrib$!\n");
}

}
elsif (-d "$LongDirectoryName\\$FILE")
{
#printf(Loghandle "\"%s\" is a 
directory.\n", $FILE);
if ( system("attrib -R 
$LongDirectoryName\\$FILE") )
{
for ($counter=0; $counter < 
$indent; $counter++) { printf(Loghandle "  "); }
printf(Loghandle "FAILED: 
folder attrib  $!\n");
}


OpenDirectory("$LongDirectoryName\\$FILE");
}
else
{
printf(Loghandle "\"%s\" is an 
unknown.\n", $FILE);
}
}

printf("Analyzed: \"%s\\%s\"\n", $LongDirectoryName, 
$FILE);

}
}
else
{
printf(Loghandle "FAILED: $!\n");
}

closedir(DIRhandle);
$indent--;
}



if (! open(Loghandle, ">$OUTPUT") )
{
die "Could not open \"$OUTPUT\" for writing: $!";
}

OpenDirectory("F:\\Inetpub\\Develop");
OpenDirectory("F:\\Inetpub\\Publish");

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: error "called to early to check prototype" using recursive function

2005-01-28 Thread John W. Krahn
S.A. Birl wrote:
2 problems, maybe related.
1) My recursive program is generating a "called to early to check
   prototype" error.  Ive Googled for answers but cannot find a solution
That could be because you spelled "too" wrong.  In any case, you should have 
the description of that warning message on your computer in the perldiag.pod file.

perldoc perldiag
And if you change the line "use warnings;" to "use diagnostics;" then perl 
will print out the complete warning message when your program runs.

perldoc diagnostics
But then why are you trying to reinvent the "traverse a directory tree" wheel 
when Perl comes with the File::Find module that does that already?

Anyway, your actual problem involves using a subroutine inside that subroutine 
with a prototype.  The easy answer is to remove the prototype.

perldoc perlsub
[snip]
   Because the intent of this feature is primarily to let you define
   ^
   subroutines that work like built-in functions, here are prototypes for
   ^
   some other functions that parse almost exactly like the corresponding
   built-in.

2) When this executes, it traverses 8 directories in F:\\Inetpub\\Develop
   then stops and traverses 8 directories in F:\\Inetpub\\Publish
   I cannot seem to figure out why it's giving up so early when there's
   more than 8 directories in each folder.
Sorry, I cannot see anything obvious and I do not have Windows to test it 
on.

use strict;
use warnings;
my $indent=0;
my $ERRORS=0;
my $OUTPUT="D:\\Logs\\IIS\\unlock_Read-Only_files.log";
sub OpenDirectory($)
{
$indent++;
my $counter=0;
my $Directory=shift;
my $FILE;
my $LongDirectoryName="";

for ($counter=1; $counter < $indent; $counter++) { printf("  "); }
Ick, ick, ick!
  print '  ' x $indent;

if ( (defined $LongDirectoryName) && ($LongDirectoryName !~ /^$/) )
{
$LongDirectoryName=sprintf("%s\\%s", $LongDirectoryName, $Directory);
 $LongDirectoryName = join '\\', $LongDirectoryName, $Directory;
Or:
 $LongDirectoryName = "$LongDirectoryName\\$Directory";

}
else
{
$LongDirectoryName=$Directory;
}
printf(Loghandle "Opening directory \"%s\":  ", $LongDirectoryName);
 print Loghandle qq(Opening directory "$LongDirectoryName":  );

if ( opendir(DIRhandle, "$LongDirectoryName") )
 if ( opendir DIRhandle, $LongDirectoryName )
perldoc -q "What's wrong with always quoting"

{
printf(Loghandle "success\n");
 print Loghandle "success\n";

while ( defined ($FILE = readdir(DIRhandle)) )
{
for ($counter=0; $counter < $indent; $counter++) { printf(Loghandle "  
"); }
  print Loghandle '  ' x $indent;

if ($FILE =~ /\.\.?$/)
{
printf(Loghandle "skipping \"%s\"\n", $FILE);
 print Loghandle qq(skipping "$FILE"\n);

}
else
{
if(-f "$LongDirectoryName\\$FILE")
{
#printf(Loghandle "\"%s\" is a file.\n", $FILE);
if ( system("attrib -R $LongDirectoryName\\$FILE") )
Are you sure that you are using system() correctly?  Is there not a built-in 
function or module that could do the same thing without using system()?


{
for ($counter=0; $counter < $indent; $counter++) { 
printf(Loghandle "  "); }
printf(Loghandle "FAILED: file attrib$!\n");
 print Loghandle '  ' x $indent, "FAILED: file attrib 
   $!\n";

Why do you think that there will be anything useful in $!?  Have you read the 
documentation for the system() function?

perldoc -f system

}
}
elsif (-d "$LongDirectoryName\\$FILE")
{
#printf(Loghandle "\"%s\" is a directory.\n", $FILE);
if ( system("attrib -R $LongDirectoryName\\$FILE") )
{
for ($counter=0; $counter < $indent; $counter++) { 
printf(Loghandle "  "); }
printf(Loghandle "FAILED: folder attrib  $!\n");
 print Loghandle '  ' x $indent, "FAILED: folder 
attrib  $!\n";


}
OpenDirectory("$LongDirectoryName\\$FILE");
}
else
{
printf(Loghandle "\"%s\" is an unknown.\n", $FILE);
 print Loghandle qq("$FILE" is an unknown.\n);

}
}
printf("Analyzed: \"%s\\%s\"\n", $LongDirectoryName, $FILE);
 print qq(Analyzed: "$LongDirectoryName\\$FILE"\n);

}
}
else
{
printf(Loghandle "FAILED: $!\n");
 print Loghandle "FAILED: $!\n";

}
closedir(DIRhandle);