-d test stops working

-d test stops working

am 17.02.2011 17:56:33 von precheca123

Hello,=0AI made a program that traverses a directory listing all the files =
in it and in it's sub directories.. root@pacman:/home# cat list_dir=0A=
=0A#!/usr/bin/perl -w use 5.010; use strict; =0A=
sub list_dir { say "Entring directory $_[0]"; chdir $_[0]=
or die "$_[0]: $!\n"; chomp(my $pwd =3D `pwd`)=
; foreach my $entry (glob "*") { if (-d $entry) {=0A=
=0A list_dir($entry); # Do not work without () }=
else { say "$pwd/$entry"; } } =
say "Leaving directory $_[0]"; } list_dir $ARGV[0];=0A--=0A=
=0ABut it appears to break in the -d test of the subdirectory 'linux':=0A=
=0Aroot@pacman:/home# /bin/ls -l tessio2/Books total 10744 -rw-r-=
-r-- 1 root root 2412061 2011-02-16 21:14 802.1Q-2005.pdf -rw-r--r-- 1=
root root 273110 2011-02-16 21:14 Artigo_SNMP.pdf drwxr-xr-x 4 root =
root 4096 2011-02-16 21:14 ASAP -rw-r--r-- 1 root root 5016115 2011=
-02-16 21:14 BSD_and_LINUX_09_2010_.pdf -rw-r--r-- 1 root root 356148=
2011-02-16 21:14 crash-course.pdf -rw-r--r-- 1 root root 266236 2011=
-02-16 21:14 frutas_na_alimentacao.pdf drwxr-xr-x 2 root root 4096 =
2011-02-16 21:14 linux -rw-r--r-- 1 root root 2605022 2011-02-16 21:14=
Milan Kundera - a insustent?vel leveza do ser.pdf -rw-r--r-- 1 root r=
oot 52717 2011-02-16 21:14 opencon06-culture.pdf drwxr-xr-x 9 root r=
oot 4096 2011-02-16 21:14 Perl drwxr-xr-x 2 root root 4096 2011-=
02-16 21:14 TMP=0A-- Here is the program output (note that it stops en=
tering directories after it leaves ASAP): root@pacman:/home# perl list=
_dir tessio2 Entring directory tessio2 /home/tessio2/bookmarks-20=
11-02-16.json Entring directory Books /home/tessio2/Books/802.1Q-=
2005.pdf /home/tessio2/Books/Artigo_SNMP.pdf Entri ng directory AS=
AP Entring directory Dic /home/tessio2/Books/ASAP/Dic/01.txt=0A=
=0A/home/tessio2/Books/ASAP/Dic/pre.txt Leaving directory Dic /ho=
me/tessio2/Books/ASAP/Distc Leaving directory ASAP /home/tessio2/=
Books/BSD_and_LINUX_09_2010_.pdf /home/tessio2/Books/cr ash-course.pdf=
/home/tessio2/Books/frutas_na_alimentacao.pdf /ho me/tessio2/Book=
s/linux=0A =0A/home/tessio2/Books/Milan Kundera - a insustent=E1vel levez=
a do ser.pdf /home/tessio2/Books/opencon06-culture.pdf /home/tess=
io2/Books/Perl /home/tessio2/Books/TMP Leaving directory Books=0A=
=0A/home/tessio2/commands /home/tessio2/concurso.pdf=0A =0A/home/tessio=
2/Desktop /home/tessio2/Documents /home/tessio2/Do wnloads /h=
ome/tessio2/examples.desktop /home/tessio2/Music / home/tessio2/pe=
rlish /home/tessio2/Pictures /home/tessio2/Public= 0A=0A/home/tess=
io2/Templates /home/tessio2/tj-apt /home/tessio2/t j-net /hom=
e/tessio2/Videos Leaving directory tessio2=0A-- Why the -d test o=
f subdirectory 'linux' (and posterior directories) don't work?=0AThanks for=
your time! =0A

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: -d test stops working

am 18.02.2011 02:34:59 von precheca123

I just found the bug..=0A--=0A#!/usr/bin/perl -w use 5.010;=0Ause stri=
ct; =0Asub list_dir { my ($dir) =3D @_; chdir $dir or die "$=
dir: $!\n"; =09chomp(my $pwd =3D `pwd`); say "$pwd/"; =09for=
each my $entry (glob "*") { =09# if entry is a directory, call list_di=
r on it =09if (-d $entry) { list_dir($entry);=09# Do not wo=
rk without "()" =09} else { say "$pwd/$entry"; =09}=0A=
=09} chdir ".." or die "..: $!\n";=0A} # main program=0Adie "$0: =
missing operand\n" if @ARGV < 1; foreach (@ARGV) { list_dir $_;=
=0A}=0A-- 'chdir ".." or die "..: $!\n"' is the bug fix.. :) > As=
sunto: -d test stops working=0A> Para: beginners@perl.org=0A> Data: Quinta-=
feira, 17 de Fevereiro de 2011, 14:56=0A> Hello,=0A> I made a program that =
traverses a directory listing all the=0A> files in it and in it's sub direc=
tories..=0A> =0A> root@pacman:/home# cat list_dir=0A> =0A> #!/usr/bin/perl =
-w=0A> =0A> =0A> =0A> use 5.010;=0A> =0A> use strict;=0A> =0A> =0A> =0A> =
=0A> sub list_dir {=0A> =0A> =A0 =A0 say "Entring directory $_[0]";=0A> =0A=
> =A0 =A0 chdir $_[0]=0A> =0A> =A0 =A0 =A0 =A0 or die "$_[0]: $!\n";=0A> =
=0A> =0A> =0A> =A0 =A0 chomp(my $pwd =3D `pwd`);=0A> =0A> =A0 =A0 foreach m=
y $entry (glob "*") {=0A> =0A> =A0 =A0 =A0 =A0 if (-d $entry) {=0A> =0A> =
=A0 =A0 =A0 =A0 =A0   > list_dir($entry);=A0 =A0 # Do not work without =
()=0A> =0A> =A0 =A0 =A0 =A0 } else {=0A> =0A> =A0 =A0 =A0 =A0 =A0 =A0 say=
=0A> "$pwd/$entry";=0A> =0A> =A0 =A0 =A0 =A0 }=0A> =0A> =A0 =A0 }=0A> =0A> =
=A0 =A0 say "Leaving directory $_[0]";=0A> =0A> }=0A> =0A> list_dir $ARGV[0=
];=0A> --=0A> =0A> But it appears to break in the -d test of the subdirecto=
ry=0A> 'linux':=0A> =0A> root@pacman:/home# /bin/ls -l tessio2/Books=0A> =
=0A> total 10744=0A> =0A> -rw-r--r-- 1 root root 2412061 2011-02-16 21:14=
=0A> 802.1Q-2005.pdf=0A> =0A> -rw-r--r-- 1 root root=A0 273110 2011-02-16 2=
1:14=0A> Artigo_SNMP.pdf=0A> =0A> drwxr-xr-x 4 root root=A0 =A0 4096 2011-0=
2-16 21:14=0A> ASAP=0A> =0A> -rw-r--r-- 1 root root 5016115 2011-02-16 21:1=
4=0A> BSD_and_LINUX_09_2010_.pdf=0A> =0A> -rw-r--r-- 1 root root=A0 356148 =
2011-02-16 21:14=0A> crash-course.pdf=0A> =0A> -rw-r--r-- 1 root root=A0 26=
6236 2011-02-16 21:14=0A> frutas_na_alimentacao.pdf=0A> =0A> drwxr-xr-x 2 r=
oot root=A0 =A0 4096 2011-02-16 21:14=0A> linux=0A> =0A> -rw-r--r-- 1 root =
root 2605022 2011-02-16 21:14 Milan=0A> Kundera - a insustent?vel leveza do=
ser.pdf=0A> =0A> -rw-r--r-- 1 root root  =A052717 2011-02-16=0A> 21:14=
opencon06-culture.pdf=0A> =0A> drwxr-xr-x 9 root root=A0 =A0 4096 2011-02-=
16 21:14=0A> Perl=0A> =0A> drwxr-xr-x 2 root root=A0 =A0 4096 2011-02-16 21=
:14=0A> TMP=0A> --=0A> =0A> Here is the program output (note that it stops =
entering=0A> directories after it leaves ASAP):=0A> =0A> root@pacman:/home#=
perl list_dir tessio2=0A> =0A> Entring directory tessio2=0A> =0A> /home/te=
ssio2/bookmarks-2011-02-16.json=0A> =0A> Entring directory Books=0A> =0A> /=
home/tessio2/Books/802.1Q-2005.pdf=0A> =0A> /home/tessio2/Books/Artigo_SNMP=
..pdf=0A> =0A> Entring directory ASAP=0A> =0A> Entring directory Dic=0A> =0A=
> /home/tessio2/Books/ASAP/Dic/01.txt=0A> =0A> /home/tessio2/Books/ASAP/Dic=
/pre.txt=0A> =0A> Leaving directory Dic=0A> =0A> /home/tessio2/Books/ASAP/D=
istc=0A> =0A> Leaving directory ASAP=0A> =0A> /home/tessio2/Books/BSD_and_L=
INUX_09_2010_.pdf=0A> =0A> /home/tessio2/Books/crash-course.pdf=0A> =0A> /h=
ome/tessio2/Books/frutas_na_alimentacao.pdf=0A> =0A> /home/tessio2/Books/li=
nux=0A>     > /home/tessio2/Books/Milan Kundera - a insustent=E1vel=
leveza=0A> do ser.pdf=0A> =0A> /home/tessio2/Books/opencon06-culture.pdf=
=0A> =0A> /home/tessio2/Books/Perl=0A> =0A> /home/tessio2/Books/TMP=0A> =0A=
> Leaving directory Books=0A> =0A> /home/tessio2/commands=0A> =0A> /home/te=
ssio2/concurso.pdf=0A> =0A> /home/tessio2/Desktop=0A> =0A> /home/tessio2/Do=
cuments=0A> =0A> /home/tessio2/Downloads=0A> =0A> /home/tessio2/examples.de=
sktop=0A> =0A> /home/tessio2/Music=0A> =0A> /home/tessio2/perlish=0A> =0A> =
/home/tessio2/Pictures=0A> =0A> /home/tessio2/Public=0A> =0A> /home/tessio2=
/Templates=0A> =0A> /home/tessio2/tj-apt=0A> =0A> /home/tessio2/tj-net=0A> =
=0A> /home/tessio2/Videos=0A> =0A> Leaving directory tessio2=0A> --=0A> =0A=
> Why the -d test of subdirectory 'linux' (and posterior=0A> directories) d=
on't work?=0A> Thanks for your time!=0A> =0A> =0A> =0A> =0A

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: -d test stops working

am 18.02.2011 17:37:18 von Shawn H Corey

On 11-02-17 08:34 PM, Téssio Fechine wrote:
> I just found the bug..

If you're traversing directories, you should use File::Find. It's a
standard module that is installed with Perl. See `perldoc File::Find`
for details.

Also, to get a list of the standard modules and pragmatics:
perldoc perlmodlib


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/