Re: globing directory names with spaces
by fruiture (Curate) on Sep 16, 2002 at 14:15 UTC
|
Imho glob()bing is ugly anyway. You don't need it. It is much better (again imho) to use grep and readdir, which is also much more portable:
my $dir = shift;
my @list = grep /\.java$/ => sub {
opendir my $dh => $_[0] or return;
my @l = readdir $dh;
closedir $dh;
return @l }->( $dir )
;
The subref is of course overhead, but you might want to keep it as named sub for multiple use.
--
http://fruiture.de | [reply] [d/l] |
|
The subref is of course overhead, but you might want to keep it as named sub for multiple use.
Or just eliminate it entirely using a do-block instead of indirecting it with a subroutine:
my $dir = shift;
my @list = grep /\.java$/ , do {
if (opendir my $dh, $dir) {
my @l = readdir $dh;
closedir $dh;
@l;
} else { () }
};
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |
Re: globing directory names with spaces
by zigdon (Deacon) on Sep 16, 2002 at 13:35 UTC
|
It seems that you have to escape the spaces in your path:
my $dir = shift;
$dir =~ s/ /\\ /g;
my @list = glob("$dir/*.java");
see perldoc -f glob for more information.
-- Dan | [reply] [d/l] |
Re: globing directory names with spaces
by choocroot (Friar) on Sep 16, 2002 at 14:11 UTC
|
you could use the quotemeta() function :
my $dir = shift;
$dir = quotemeta( $dir );
@list = glod( "$dir/*.java" );
Using the angle brackets operator seems to work without quoting :
my $dir = shift;
@list = <"$dir/*.java">;
| [reply] [d/l] [select] |
|
works very well.
But it list *.java files only of the directory which I specify.
What can I do to make it list all the *.java diles in the direcotry which I specify and all its subdirectories as well?
thanks for your reply.
regards,
Abhishek.
| [reply] |
|
use File::Find;
my @java_file;
find(sub {
push @java_file, $File::Find::name if /\.java$/i
}, "/desired/path");
Makeshifts last the longest. | [reply] [d/l] |
|
|
|
|
If you are getting that complicated maybe you should look at File::Find.
-- flounder
| [reply] |
Re: globing directory names with spaces
by samurai (Monk) on Sep 16, 2002 at 15:52 UTC
|
Might wanna just read the directory with DirHandle instead of going all out with File::Find as suggested:
use DirHandle;
my $dh = new DirHandle($dir) or die "Can't open $dir: $!";
my @files = ();
push @files, $_ while $_ = $dh->read();
--
perl: code of the samurai | [reply] [d/l] |
|
| [reply] |
Re: globing directory names with spaces
by kschwab (Vicar) on Sep 16, 2002 at 20:12 UTC
|
Another option is to use File::Glob's bsd_glob
instead of CORE::glob.
File::Glob is having trouble with the spaces because it's
designed to...per the docs:
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 pat tern.
See the node called globing files with spaces problem for more info. | [reply] |