Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: list two paths files into one array (updated)

by haukex (Archbishop)
on Nov 13, 2017 at 20:44 UTC ( [id://1203298]=note: print w/replies, xml ) Need Help??


in reply to list two paths files into one array

The built-in glob splits its arguments on whitespace, so one might think of this as a possibility, but especially since you seem to be on Windows, I wouldn't be surprised if the pathnames contain spaces. I would recommend bsd_glob from File::Glob instead, which does not split on whitespace:

use File::Glob 'bsd_glob'; my @files = ( bsd_glob("$Dir1/*"), bsd_glob("$Dir2/*") );

You can also use File::Glob ':bsd_glob'; to override the builtin glob (except on really old versions of Perl). Another possible caveat of glob to be aware of is that it does not list files beginning with a dot by default.

Update: Another possibility might be: my @files = bsd_glob("{$Dir1,$Dir2}/*"); Also a few edits to add a bit of info.

Update 2: An even bigger possible caveat that should be mentioned is that of course all this assumes that $Dir1 and $Dir2 don't contain any characters that have special meaning to glob. If that is the case, I'd recommend other methods like Path::Class::Dir's children method (this will include filenames beginning with a dot, use ->children(no_hidden => 1) to suppress them):

use Path::Class qw/dir/; my @files = ( dir($Dir1)->children, dir($Dir2)->children );

Replies are listed 'Best First'.
Re^2: list two paths files into one array (updated)
by ytjPerl (Scribe) on Nov 13, 2017 at 21:03 UTC
    Thanks, File::Glob 'bsd_glob' works

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1203298]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found