Dir::ls - List the contents of a directory
use Dir::ls; print "$_\n" for ls; # defaults to current working directory print "$_: ", -s "/foo/bar/$_", "\n" for ls '/foo/bar', {all => 1, sort => 'size'};
Provides the function "ls", which returns the contents of a directory in a similar manner to the GNU coreutils command ls(1).
my @contents = ls $dir, \%options;
Takes a directory path and optional hashref of options, and returns a list of items in the directory. Home directories represented by ~ will be expanded by "path" in Path::Tiny. If no directory path is passed, the current working directory will be used. Like in ls(1), the returned names are relative to the passed directory path, so if you want to use a filename (such as passing it to open or stat), you must prefix it with the directory path, with ~ expanded if present.
~
open
stat
# Check the size of a file in current user's home directory my @contents = ls '~'; say -s glob('~') . "/$contents[0]";
By default, hidden files and directories (those starting with .) are omitted, and the results are sorted by name according to the current locale (see perllocale for more information).
.
Accepts the following options:
Include hidden files and directories.
Include hidden files and directories, but not . or ...
..
Sort by ctime (change time) in seconds since the epoch.
Equivalent to passing all and setting sort to none.
all
sort
none
Reverse sort order (unless U or sort => 'none' specified).
U
sort => 'none'
Specify sort algorithm other than the default sort-by-name. Valid values are: none, extension, size, time, or version.
extension
size
time
version
Sort by file size in bytes (descending). Equivalent to sort => 'size'.
sort => 'size'
Sort by mtime (modification time) in seconds since the epoch. Equivalent to sort => 'time'.
sort => 'time'
Sort by atime (access time) in seconds since the epoch.
Return entries in directory order (unsorted). Equivalent to sort => 'none'.
Sort naturally by version numbers within the name. Uses Sort::filevercmp for sorting. Equivalent to sort => 'version'.
sort => 'version'
Sort by (last) file extension, according to the current locale. Equivalent to sort => 'extension'.
sort => 'extension'
This is only an approximation of ls(1). It makes an attempt to give the same output under the supported options, but there may be differences in edge cases. Weird things might happen with sorting of non-ASCII filenames, or on non-Unixlike systems. Lots of options aren't supported yet. Patches welcome.
Report any issues on the public bugtracker.
Dan Book <dbook@cpan.org>
This software is Copyright (c) 2017 by Dan Book.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
Path::Tiny, ls(1)
To install Dir::ls, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dir::ls
CPAN shell
perl -MCPAN -e shell install Dir::ls
For more information on module installation, please visit the detailed CPAN module installation guide.