|
From: annesville on 5 Feb 2007 06:41 Hello All, I am still learning Perl. I am trying to write a script to recursively list all the directories starting from a specified path. Unfortunately it seems that the globbing function handles paths with/ without spaces in them very differently as shown by the test case script below. Some research seems seems to indicate that I would be better off using opendir/readdir. Accepted, but I would like, as part of my learning experience, for someone to explain in simple terms WHY the below script handles different paths differently. Please note that I have used literal paths, but in application I would obviously need to use a variable - so if you suggest escaping the space, it is not so simple (although it is do-able), and doesn't make any difference anyway. I am using ActivePerl 5.8.8.820 Regards, Nicolas ----- START PERL ----- use strict; use warnings; my $file; print "--- Windows, unquoted ---\n"; # Returns a list of all files and directories in 'C:\Windows' foreach $file (<c:/windows/*>) {print "$file\n";}; print "--- Windows, quoted ---\n"; # Returns nothing foreach $file (<"c:/windows/*">) {print "$file\n";}; print "--- Program Files, unquoted ---\n"; # Returns 'c:/program' as the only result foreach $file (<c:/program files/*>) {print "$file\n";}; print "--- Program Files, quoted ---\n"; # Returns a list of all the files and directories in 'C:\Program Files' foreach $file (<"c:/program files/*">) {print "$file\n";}; ----- END PERL -----
From: Michele Dondi on 5 Feb 2007 09:36 On 5 Feb 2007 03:41:04 -0800, annesville(a)hotmail.com wrote: >Hello All, > >I am still learning Perl. I am trying to write a script to >recursively list all the directories starting from a specified path. ^^^^^^^^^^^ ^^^^^^^^^^^ This calls for File::Find or some of its cousins, like File::Find::Rule or File::Finder. If you insist on reinventing the wheel, you may still look into them to see how they are implemented. >Unfortunately it seems that the globbing function handles paths with/ >without spaces in them very differently as shown by the test case >script below. It's all duly explained in perldoc File::Glob : Since v5.6.0, Perl's CORE::glob() is implemented in terms of : bsd_glob(). Note that they don't share the same : prototype--CORE::glob() only accepts a single argument. Due to : historical reasons, CORE::glob() will also split its argument on : whitespace, treating it as multiple patterns, whereas bsd_glob() : considers them as one pattern. >Some research seems seems to indicate that I would be better off using >opendir/readdir. Accepted, but I would like, as part of my learning In some situations you prefer glob(), in some other opendir/readdir. The latter is certainly more verbose, but in some cases it may be needed if you want maximum flexibility. >experience, for someone to explain in simple terms WHY the below >script handles different paths differently. Please note that I have >used literal paths, but in application I would obviously need to use a >variable - so if you suggest escaping the space, it is not so simple >(although it is do-able), and doesn't make any difference anyway. See above. >print "--- Windows, unquoted ---\n"; # Returns a list of all files and >directories in 'C:\Windows' >foreach $file (<c:/windows/*>) {print "$file\n";}; BTW: I would use angular parentheses like that all the time in one-liners. But definitely *not* in any longer code. HTH, Michele -- {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_, 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
From: annesville on 6 Feb 2007 01:36 Thanks for the pointers Michelle. Nicolas
From: Michele Dondi on 6 Feb 2007 03:12 On 5 Feb 2007 22:36:22 -0800, annesville(a)hotmail.com wrote: >Thanks for the pointers Michelle. Not that I'm bothered or offended, but... Michele (italian masculine name as opposed to Michelle, which is a french feminine one). Michele -- {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_, 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
From: annesville on 6 Feb 2007 09:13 On Feb 6, 10:12 am, Michele Dondi <bik.m...(a)tiscalinet.it> wrote: > On 5 Feb 2007 22:36:22 -0800, annesvi...(a)hotmail.com wrote: > > >Thanks for the pointers Michelle. > > Not that I'm bothered or offended, but... Michele (italian masculine > name as opposed to Michelle, which is a french feminine one). > > Michele > -- > {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr > (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' > .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_, > 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH, Whoa! Sorry 'bout that. I obviously don't qualify as a citizen of the global community yet! BTW, I am sure there must be a shorter way to write your signiture :-) Nicolas
|
Next
|
Last
Pages: 1 2 Prev: rename *-ch03.htm to ch03-*.htm? Next: Net::Telnet Error handling/trapping |