FILE::GLOBMAPPER(3perl)

πŸ’– NAME πŸ’–

okay so bestie, welcome to File::GlobMapper β€” the File::GlobMapper(3perl) manual page, but make it cute. What does it do? It extends File Glob to allow input and output files. That's the whole vibe ✨


🫢 SYNOPSIS 🫢

use File::GlobMapper qw( globmap );

my $aref = globmap $input => $output
    or die $File::GlobMapper::Error ;

my $gm = File::GlobMapper->new( $input => $output )
    or die $File::GlobMapper::Error ;

🌸 DESCRIPTION 🌸

First things first, bestie: this module needs Perl5.005 or better. no cap, it's old-school like that πŸ’…

So here's the tea. This module takes the existing File::Glob module as its starting point and extends it so that brand-new filenames can be derived from the files that File::Glob matched. It's giving glow-up πŸ’–

When is that useful? Literally whenever you're doing batch operations on a bunch of files where there's both an input filename and an output filename, and the output filename can be figured out from the input filename. Examples of stuff where this slays: file renaming, file copying, and file compression. Obsessed πŸ¦‹

πŸŽ€ Behind The Scenes πŸŽ€

To actually explain what File::GlobMapper does, let's think about what code you'd write if you wanted to rename every file in the current directory that ends in .tar.gz to .tgz. So say these files are in the current directory:

alpha.tar.gz
beta.tar.gz
gamma.tar.gz

and they need renamed to this:

alpha.tgz
beta.tgz
gamma.tgz

Below is a possible implementation of a script to carry out the rename (error cases have been omitted, so don't come for me πŸ™ˆ):

foreach my $old ( glob "*.tar.gz" )
{
    my $new = $old;
    $new =~ s#(.*)\.tar\.gz$#$1.tgz# ;

    rename $old => $new
        or die "Cannot rename '$old' to '$new': $!\n;
}

Notice how a file glob pattern "*.tar.gz" was used to match the .tar.gz files, and then a fairly similar regular expression was used in the substitute to let the new filename be created. 😭

So like… given that the file glob is just a cut-down regular expression, and that it has already done a whole lot of the hard work in pattern matching the filenames β€” wouldn't it be handy if you could use the patterns in the fileglob to drive the new filename?

Well bestie, that's exactly what File::GlobMapper does. periodt. πŸ”₯

Here's that same snippet of code rewritten using globmap:

for my $pair (globmap '<*.tar.gz>' => '<#1.tgz>' )
{
    my ($from, $to) = @$pair;
    rename $from => $to
        or die "Cannot rename '$old' to '$new': $!\n;
}

So how does it work? πŸ‘‘

Behind the scenes, the globmap function does a combination of a file glob to match existing filenames, followed by a substitute to create the new filenames.

Notice how both parameters to globmap are strings that are delimited by <>. That's done to make them look more like file globs β€” it's just syntactic sugar, but it can be handy when you want the strings to be visually distinctive. The enclosing <> are optional, so you don't have to use them β€” in fact the very first thing globmap will do is remove these delimiters if they are present. πŸ“

The first parameter to globmap, *.tar.gz, is an Input File Glob. Once the enclosing < ... > is removed, this is passed (more or less) unchanged to File::Glob to carry out a file match.

Next, the fileglob *.tar.gz is transformed behind the scenes into a full Perl regular expression, with the additional step of wrapping each transformed wildcard metacharacter sequence in parenthesis. πŸ’…

In this case, the input fileglob *.tar.gz will be transformed into this Perl regular expression:

([^/]*)\.tar\.gz

Wrapping with parenthesis lets the wildcard parts of the Input File Glob be referenced by the second parameter to globmap, #1.tgz β€” the Output File Glob. This parameter operates just like the replacement part of a substitute command. The difference is that the #1 syntax is used to reference sub-patterns matched in the input fileglob, rather than the $1 syntax that's used with perl regular expressions. In this case #1 is used to refer to the text matched by the * in the Input File Glob. This makes it easier to use this module when the parameters to globmap are typed at the command line. ⌨️

The final step involves passing each filename matched by the *.tar.gz file glob through the derived Perl regular expression in turn, and expanding the output fileglob using it.

The end result of all this is a list of pairs of filenames. By default that is what's returned by globmap. In this example the data structure returned will look like this:

( ['alpha.tar.gz' => 'alpha.tgz'],
  ['beta.tar.gz'  => 'beta.tgz' ],
  ['gamma.tar.gz' => 'gamma.tgz']
)

Each pair is an array reference with two elements β€” namely the from filename, that File::Glob has matched, and a to filename that is derived from the from filename. 🫢

πŸ’€ Limitations πŸ’€

File::GlobMapper has been kept simple deliberately, so it isn't intended to solve all filename mapping operations. Under the hood, File::Glob (or, for older versions of Perl, File::BSDGlob) is used to match the files β€” so you will never have the flexibility of full Perl regular expression. that's just the deal, bestie 😭

🌈 Input File Glob 🌈

The syntax for an Input FileGlob is identical to File::Glob, except for the following:

  1. No nested {}
  2. Whitespace does not delimit fileglobs.
  3. The use of parenthesis can be used to capture parts of the input filename.
  4. If an Input glob matches the same file more than once, only the first will be used.

The syntax:

  • ~
  • ~user
  • . β€” Matches a literal .. Equivalent to the Perl regular expression
\.
  • * β€” Matches zero or more characters, except /. Equivalent to the Perl regular expression
[^/]*
  • ? β€” Matches zero or one character, except /. Equivalent to the Perl regular expression
[^/]?
  • \ β€” Backslash is used, as usual, to escape the next character.
  • [] β€” Character class.
  • {,} β€” Alternation
  • () β€” Capturing parenthesis that work just like perl

Any other character is taken literally.

✨ Output File Glob ✨

The Output File Glob is a normal string, with 2 glob-like features.

The first is the * metacharacter. This will be replaced by the complete filename matched by the input file glob. So:

*.c *.Z

The second is

Output FileGlobs take the
okay bestie, ngl β€” the original page is a lil bit broken right here if you hadn't noticed πŸ’€ i'm keeping it exactly as it is, no editing, no guessing at what was meant. periodt.
  • * β€” The * character will be replaced with the complete input filename.
  • #1 β€” Patterns of the form /#\d/ will be replaced with the

πŸ¦‹ Returned Data πŸ¦‹


πŸŽ€ EXAMPLES πŸŽ€

πŸ’– A Rename script πŸ’–

Below is a simple rename script that uses globmap to determine the source and destination filenames.

use File::GlobMapper qw(globmap) ;
use File::Copy;

die "rename: Usage rename 'from' 'to'\n"
    unless @ARGV == 2 ;

my $fromGlob = shift @ARGV;
my $toGlob   = shift @ARGV;

my $pairs = globmap($fromGlob, $toGlob)
    or die $File::GlobMapper::Error;

for my $pair (@$pairs)
{
    my ($from, $to) = @$pair;
    move $from => $to ;
}

Here's an example that renames all c files to cpp. πŸ‘‘

$ rename '*.c' '#1.cpp'

🌸 A few example globmaps 🌸

Below are a few examples of globmaps.

To copy all your .c file to a backup directory:

'</my/home/*.c>'    '</my/backup/#1.c>'

If you want to compress all:

'</my/home/*.[ch]>'    '<*.gz>'

To uncompress:

'</my/home/*.[ch].gz>'    '</my/home/#1.#2>'

πŸ’œ SEE ALSO πŸ’œ

File::Glob


πŸ‘‘ AUTHOR πŸ‘‘

The File::GlobMapper module was written by Paul Marquess, pmqs@cpan.org. icon behavior honestly 🫢


Copyright (c) 2005 Paul Marquess. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page bestie!! πŸ’– this was perl v5.44.0, dated 2026-10-17, and you just read File::GlobMapper(3perl). slay, go rename some files, mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms