Skip to content

Commit 9085b4e

Browse files
scottchiefbakerkhwilliamson
authored andcommitted
Add some examples to the glob() documentation
1 parent 55f5e76 commit 9085b4e

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

pod/perlfunc.pod

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,12 +3353,32 @@ X<glob> X<wildcard> X<filename, expansion> X<expand>
33533353
=for Pod::Functions expand filenames using wildcards
33543354

33553355
In list context, returns a (possibly empty) list of filename expansions on
3356-
the value of EXPR such as the standard Unix shell F</bin/csh> would do. In
3356+
the value of EXPR such as the Unix shell Bash would do. In
33573357
scalar context, glob iterates through such filename expansions, returning
3358-
undef when the list is exhausted. This is the internal function
3359-
implementing the C<< <*.c> >> operator, but you can use it directly. If
3360-
EXPR is omitted, L<C<$_>|perlvar/$_> is used. The C<< <*.c> >> operator
3361-
is discussed in more detail in L<perlop/"I/O Operators">.
3358+
L<C<undef>|/undef EXPR> when the list is exhausted. If EXPR is omitted,
3359+
L<C<$_>|perlvar/$_> is used.
3360+
3361+
# List context
3362+
my @txt_files = glob("*.txt");
3363+
my @perl_files = glob("*.pl *.pm");
3364+
3365+
# Scalar context
3366+
while (my $file = glob("*.mp3")) {
3367+
# Do stuff
3368+
}
3369+
3370+
Glob also supports an altnernate syntax using C<< < >> C<< > >> as
3371+
delimiters. While this syntax is supported, it is recommended that you
3372+
use C<glob> instead as it is more readable and searchable.
3373+
3374+
my @txt_files = <"*.txt">;
3375+
3376+
If you need case insensitive file globbing that can be achieved using the
3377+
C<:nocase> parameter of the L<C<bsd_glob>|File::Glob/C<bsd_glob>> module.
3378+
3379+
use File::Glob qw(:globally :nocase);
3380+
3381+
my @txt = glob("readme*"); # README readme.txt Readme.md
33623382

33633383
Note that L<C<glob>|/glob EXPR> splits its arguments on whitespace and
33643384
treats
@@ -3371,23 +3391,23 @@ For example, to glob filenames that have an C<e> followed by a space
33713391
followed by an C<f>, use one of:
33723392

33733393
my @spacies = <"*e f*">;
3374-
my @spacies = glob '"*e f*"';
3375-
my @spacies = glob q("*e f*");
3394+
my @spacies = glob('"*e f*"');
3395+
my @spacies = glob(q("*e f*"));
33763396

33773397
If you had to get a variable through, you could do this:
33783398

3379-
my @spacies = glob "'*${var}e f*'";
3380-
my @spacies = glob qq("*${var}e f*");
3399+
my @spacies = glob("'*${var}e f*'");
3400+
my @spacies = glob(qq("*${var}e f*"));
33813401

33823402
If non-empty braces are the only wildcard characters used in the
33833403
L<C<glob>|/glob EXPR>, no filenames are matched, but potentially many
33843404
strings are returned. For example, this produces nine strings, one for
33853405
each pairing of fruits and colors:
33863406

3387-
my @many = glob "{apple,tomato,cherry}={green,yellow,red}";
3407+
my @many = glob("{apple,tomato,cherry}={green,yellow,red}");
33883408

33893409
This operator is implemented using the standard C<File::Glob> extension.
3390-
See L<File::Glob> for details, including
3410+
See L<C<bsd_glob>|File::Glob/C<bsd_glob>> for details, including
33913411
L<C<bsd_glob>|File::Glob/C<bsd_glob>>, which does not treat whitespace
33923412
as a pattern separator.
33933413

@@ -3398,6 +3418,12 @@ is used as a C<while>/C<for> condition, then the condition actually
33983418
tests for definedness of the expression's value, not for its regular
33993419
truth value.
34003420

3421+
Internal implemenation details:
3422+
3423+
This is the internal function implementing the C<< <*.c> >> operator,
3424+
but you can use it directly. The C<< <*.c> >> operator is discussed in
3425+
more detail in L<perlop/"I/O Operators">.
3426+
34013427
Portability issues: L<perlport/glob>.
34023428

34033429
=item gmtime EXPR

0 commit comments

Comments
 (0)