develooper Front page | perl.perl5.porters | Postings from April 2000

[ID 20000413.006] Problem with recursive calls

Thread Next
From:
mckaya
Date:
April 13, 2000 12:19
Subject:
[ID 20000413.006] Problem with recursive calls
Message ID:
200004131918.PAA14553@zarathustra.arl.qwestip.net

This is a bug report for perl from elata.ahmed@qwest.com,
generated with the help of perlbug 1.20 running under perl 5.00404.



Hi all,

In an attemp to traverse a directory tree recursively, I wrote the following perl program:
 ( I stripped it to focus on the problem):



#!/bin/perl -w
process_it( "/home/mckaya/dir1");
#process_it( "/usr");




sub process_it
    {
    my @files;
        print " inside sub  for : $_[0]\n";
        opendir (my_DIR,"$_[0]");
        @files= grep !/^\./, readdir(my_DIR);
        foreach $k (@files) {
                print " $k";
        }
        print "\n";
        foreach $fn  (@files){
                print " $fn  $_[0]  \n";
                if ( -d  "$_[0]/$fn"  )
                {
                        $a="$_[0]/$fn";
                        print  " $a is a directory\n";
                        system ("ls -ld $a");
                        process_it ($a);
                   }
                else {

                        print   " $_[0]/$fn is a file:\n";
                        system (" ls -l \"$_[0]/$fn\"");
                     }
        print  "$_[0]/$fn \n";
      }
close (my_DIR);
print " leaving sub for $_[0]\n";
}



The problem is that: whenever a deep branch is completed ( after a leaf is reached)
the argument tracking seem to be wrong. By the time it returns to the parent of that child, it still
stores the child's argument and deal with it as if it is the argument for the current call (parent).
It does not pop the argument off the stack.

I modified the program to be as follows:



#!/bin/perl -w
process_it( "/home/mckaya/dir1");
#process_it( "/usr");




sub process_it
    {
    my @files;
    my $current;
    $current = $_[0];
        print " inside sub  for : $_[0]\n";
        opendir (my_DIR,"$current");
        @files= grep !/^\./, readdir(my_DIR);
        foreach $k (@files) {
                print " $k";
        }
        print "\n";
        foreach $fn  (@files){
                print " $fn  $current  \n";
                if ( -d  "$current/$fn"  )
                {
                        $a="$current/$fn";
                        print  " $a is a directory\n";
                        system ("ls -ld $a");
                        process_it ($a);
                   }
                else {

                        print   " $current/$fn is a file:\n";
                        system (" ls -l \"$current/$fn\"");
                     }
        print  "$current/$fn \n";
      }
close (my_DIR);
print " leaving sub for $current\n";
}
 


After the modifications the program ran correctly.

What I did is that I stored the argument in a local variable ( $current), rather than
dealing directly with the argument.


If This is my stupid mistake, please forgive me for that, otherwise this may be  a bug.


Thanks..


Elata Ahmed

04-13-00

elata.ahmed@qwest.com

---
Site configuration information for perl 5.00404:

Configured by stevec at Wed Jul  8 01:35:11 EDT 1998.

Summary of my perl5 (5.0 patchlevel 4 subversion 4) configuration:
  Platform:
    osname=solaris, osvers=2.6, archname=sun4-solaris
    uname='sunos 5.6 generic sun4u sparc sunw,ultra-1 '
    hint=recommended, useposix=true, d_sigaction=define
    bincompat3=y useperlio=undef d_sfio=undef
  Compiler:
    cc='gcc', optimize='-O', gccversion=2.8.1
    cppflags='-I/usr/local/include'
    ccflags ='-I/usr/local/include'
    stdchar='unsigned char', d_stdstdio=define, usevfork=false
    voidflags=15, castflags=0, d_casti32=define, d_castneg=define
    intsize=4, alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib /usr/ccs/lib
    libs=-lsocket -lnsl -ldl -lm -lc -lcrypt
    libc=/lib/libc.so, so=so
    useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-fpic', lddlflags='-G -L/usr/local/lib'

Locally applied patches:
	

---
@INC for perl 5.00404:
	/usr/local/lib/perl5/sun4-solaris/5.00404
	/usr/local/lib/perl5
	/usr/local/lib/perl5/site_perl/sun4-solaris
	/usr/local/lib/perl5/site_perl
	.

---
Environment for perl 5.00404:
    HOME=/
    LANG (unset)
    LD_LIBRARY_PATH=/opt/BKgnome/lib:/usr/local/lib
    LOGDIR (unset)
    PATH=/opt/ipf/bin:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/openwin/bin:/usr/dt/bin:/usr/ccs/bin:/netapp/tools/bin:/netapp/tools/sbin:/usr/sbin:/usr/bin
    PERL_BADLANG (unset)
    SHELL=/usr/local/bin/bash

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About