Hi Christopher, I just dealt with a corrolary issue--how NOT to pass a file handle, which seems to happen by default if I declare the handle inline without scalar notation: open(MY_FILE, $FileName). In a recursive routine such as:
sub ListFilesContainingText { #... variable declareations et al. opendir DIR, $currentDir or die "Cannot opendir $currentDir: "; my $testFile = ""; my $file = ""; while (defined($file = readdir DIR) and $file ne "") { $testFile = "$currentDir\\$file"; if ( $testFile and (-d $testFile)) { if ($file eq "\." || $file eq "\.\.") {; } else { ListFilesContainingText($testFile, $targetText, ($indentation + 2)); } } else { #...... other non-recursive stuff for "leaf" files } } } the global nature of the DIR handle toasts the code. The code navigates up the first branch at each level and never resumes its browse of the parent folder. I amended this by using a local variable yo receive the filehandle at each layer: sub ListFilesContainingText { #... variable declareations et al. my $DIR; opendir $DIR, $currentDir or die "Cannot opendir $currentDir: "; my $testFile = ""; my $file = ""; while (defined($file = readdir $DIR) and $file ne "") { $testFile = "$currentDir\\$file"; if ( $testFile and (-d $testFile)) { if ($file eq "\." || $file eq "\.\.") {; } else { ListFilesContainingText($testFile, $targetText, ($indentation + 2)); } } else { #...... other non-recursive stuff for "leaf" files } } } Result: Each layer has its own $DIR preserved in its activation frame. When the recursed function returns, its parent still has its marker into the parent directory, and resumes its reading where it left off, and navigates through the entire directory tree. Joseph christopher j bottaro wrote: > nm, figured it out. you can pass open a scalar like this: > > open($myfile, "blah"); > ParseOneItem($myfile); > > and use it in the sub like this: > sub ParseOneItem() { > my $fh = shift(); > my $line = <$fh>; > } > > =) > > -- christopher > > On Saturday 14 December 2002 03:10 am, christopher j bottaro wrote: > > hello, > > i want to make a sub in perl that is to be used like this: > > my @array; > > open(INFILE, "blah"); > > ParseOneItem (INFILE, \@array); > > > > i know how to pass the array reference, but how do i pass the file handle? > > can i do it just like that? and how do i access it once in the sub? do i > > just shift() it outta @_ into a scalar? > > > > thanks, > > christopher > > _______________________________________________ > > Shell.scripting mailing list > > [EMAIL PROTECTED] > > http://moongroup.com/mailman/listinfo/shell.scripting > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]