Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

globing directory names with spaces

by abhishes (Friar)
on Sep 16, 2002 at 13:22 UTC ( #198235=perlquestion: print w/ replies, xml ) Need Help??
abhishes has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

When I make a call

my @list = glob("*.java");

it works fine.

but when I do a

my $dir = shift; my @list = glob("$dir/*.java");

and call my program like

perl lc.pl "c:/program files/my apps/java"

I do not get any file list.

does the glob function accept directory names with spaces?

regards,
Abhishek.

Edited: ~Mon Sep 16 16:39:07 2002 (GMT) by footpad: Added <code> tags, per Consideration

Comment on globing directory names with spaces
Select or Download Code
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

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">;
      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.
        Then you need to look into File::Find.
        use File::Find; my @java_file; find(sub { push @java_file, $File::Find::name if /\.java$/i }, "/desired/path");

        Makeshifts last the longest.

        If you are getting that complicated maybe you should look at File::Find.

        --

        flounder

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
      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

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

      Except he asked how to get the files not only in that directory but in the entire tree below it, something DirHandle won't do for him.

      Makeshifts last the longest.

Re: globing directory names with spaces
by kschwab (Priest) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://198235]
Approved by George_Sherston
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-04-19 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls