Text::Glob(3)

πŸ’… Text::Glob(3) β€” bestie edition

Okay so real talk: this is the Text::Glob(3) manual page, and it's about matching globbing patterns against text β€” not against your filesystem. Buckle up, it's actually so good.


✨ NAME ✨

Text::Glob β€” match globbing patterns against text πŸŽ€


🌸 SYNOPSIS 🌸

use Text::Glob qw( match_glob glob_to_regex );

print "matched\n" if match_glob( "foo.*", "foo.bar" );

# prints foo.bar and foo.baz
my $regex = glob_to_regex( "foo.*" );
for ( qw( foo.bar foo.baz foo bar ) ) {
    print "matched: $_\n" if /$regex/;
}

πŸ’– DESCRIPTION πŸ’–

So Text::Glob implements glob(3)-style matching, but here's the twist β€” it matches against text, not by going and fetching actual names out of a filesystem. It's giving "glob energy, but make it stringy." ✨

Heads up bestie: if you want to do full file globbing, use the File::Glob module instead. This one is not that. 🫢

🧚 Routines

  • match_glob( $glob, @things_to_test ) β€” gives you back the list of things from the source list that match the glob. Slay.
  • glob_to_regex( $glob ) β€” gives you back a compiled regex that's the equivalent of the globbing pattern.
  • glob_to_regex_string( $glob ) β€” gives you back a regex string that's the equivalent of the globbing pattern. (Yes, a string, not a compiled regex. Both exist. Iconic.)

πŸ”₯ SYNTAX πŸ”₯

These are the metacharacters and rules that are respected:

  • * β€” matches zero or more characters
    • a* matches a, aa, aaaa and like, many many more. infinite vibes.
  • ? β€” matches exactly one character
    • a? matches aa, but not a, and not aaa. one (1) character, periodt.
  • Character sets/ranges
    • example.[ch] matches example.c and example.h
    • demo.[a-c] matches demo.a, demo.b, and demo.c
  • alternation
    • example.{foo,bar,baz} matches example.foo, example.bar, and example.baz
  • leading . must be explicitly matched
    • *.foo does not match .bar.foo. For this you must either specify the leading . in the glob pattern (.*.foo), or set $Text::Glob::strict_leading_dot to a false value while compiling the regex.
  • * and ? do not match the seperator (i.e. they do not match /)
    • *.foo does not match bar/baz.foo. For this you must either explicitly match the / in the glob (*/*.foo), or set $Text::Glob::strict_wildcard_slash to a false value while compiling the regex, or change the seperator that Text::Glob uses by setting $Text::Glob::seperator to an alternative value while compiling the the regex.

πŸ’€ BUGS πŸ’€

The code uses qr// to produce compiled regexes, therefore this module requires perl version 5.005_03 or newer. That's the whole bug note. No cap.


πŸ‘‘ AUTHOR πŸ‘‘

Richard Clamp <richardc@unixbeard.net>


Copyright (C) 2002, 2003, 2006, 2007 Richard Clamp. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


πŸ¦‹ SEE ALSO πŸ¦‹

File::Glob, glob(3)


And that's the whole page, bestie β€” brought to you by perl v5.42.0, dated 2025-05-20. Go forth and match some globs πŸ’…βœ¨

πŸ“¦ where this page comes from