perl - directory tree warning -
i have writed script, recursively print's directory's content. prints warning each folder. how fix this?
sample folder:
dev# cd /tmp/test
dev# ls -p -r
test2/
testfile
testfile2./test2:
testfile3
testfile4
my code:
#!/usr/bin/perl use strict; use warnings; browsedir('/tmp/test'); sub browsedir { $path = shift; opendir(my $dir, $path); while (readdir($dir)) { next if /^\.{1,2}$/; if (-d "$path/$_") { browsedir("$path/$_"); } print "$path/$_\n"; } closedir($dir); }
and output:
dev# perl /tmp/cotest.pl
/tmp/test/test2/testfile3
/tmp/test/test2/testfile4
use of uninitialized value $_ in concatenation (.) or string @ /tmp/cotest.pl line 16.
/tmp/test/
/tmp/test/testfile
/tmp/test/testfile2
may try code:
#!/usr/bin/perl use strict; use warnings; browsedir('/tmp'); sub browsedir { $path = shift; opendir(my $dir, $path); while (readdir($dir)) { next if /^\.{1,2}$/; print "$path/$_\n"; if (-d "$path/$_") { browsedir("$path/$_"); } } closedir($dir); }
if got error, because call browsedir() before use variable $_.
Comments
Post a Comment