{
    "mode": "info",
    "parameter": "File::GlobMapper",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/File%3A%3AGlobMapper/json",
    "generated": "2026-07-05T11:50:54Z",
    "synopsis": "use File::GlobMapper qw( globmap );\nmy $aref = globmap $input => $output\nor die $File::GlobMapper::Error ;\nmy $gm = File::GlobMapper->new( $input => $output )\nor die $File::GlobMapper::Error ;",
    "sections": {
        "File::GlobMapper(3perl)Perl Programmers Reference GuideFile::GlobMapper(3perl)": {
            "content": "",
            "subsections": []
        },
        "NAME": {
            "content": "File::GlobMapper - Extend File Glob to Allow Input and Output Files\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use File::GlobMapper qw( globmap );\n\nmy $aref = globmap $input => $output\nor die $File::GlobMapper::Error ;\n\nmy $gm = File::GlobMapper->new( $input => $output )\nor die $File::GlobMapper::Error ;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module needs Perl5.005 or better.\n\nThis module takes the existing \"File::Glob\" module as a starting point\nand extends it to allow new filenames to be derived from the files\nmatched by \"File::Glob\".\n\nThis can be useful when carrying out batch operations on multiple files\nthat have both an input filename and output filename and the output\nfile can be derived from the input filename. Examples of operations\nwhere this can be useful include, file renaming, file copying and file\ncompression.\n\nBehind The Scenes\nTo help explain what \"File::GlobMapper\" does, consider what code you\nwould write if you wanted to rename all files in the current directory\nthat ended in \".tar.gz\" to \".tgz\". So say these files are in the\ncurrent directory\n\nalpha.tar.gz\nbeta.tar.gz\ngamma.tar.gz\n\nand they need renamed to this\n\nalpha.tgz\nbeta.tgz\ngamma.tgz\n\nBelow is a possible implementation of a script to carry out the rename\n(error cases have been omitted)\n\nforeach my $old ( glob \"*.tar.gz\" )\n{\nmy $new = $old;\n$new =~ s#(.*)\\.tar\\.gz$#$1.tgz# ;\n\nrename $old => $new\nor die \"Cannot rename '$old' to '$new': $!\\n;\n}\n\nNotice that a file glob pattern \"*.tar.gz\" was used to match the\n\".tar.gz\" files, then a fairly similar regular expression was used in\nthe substitute to allow the new filename to be created.\n\nGiven that the file glob is just a cut-down regular expression and that\nit has already done a lot of the hard work in pattern matching the\nfilenames, wouldn't it be handy to be able to use the patterns in the\nfileglob to drive the new filename?\n\nWell, that's exactly what \"File::GlobMapper\" does.\n\nHere is same snippet of code rewritten using \"globmap\"\n\nfor my $pair (globmap '<*.tar.gz>' => '<#1.tgz>' )\n{\nmy ($from, $to) = @$pair;\nrename $from => $to\nor die \"Cannot rename '$old' to '$new': $!\\n;\n}\n\nSo how does it work?\n\nBehind the scenes the \"globmap\" function does a combination of a file\nglob to match existing filenames followed by a substitute to create the\nnew filenames.\n\nNotice how both parameters to \"globmap\" are strings that are delimited\nby <>.  This is done to make them look more like file globs - it is\njust syntactic sugar, but it can be handy when you want the strings to\nbe visually distinctive. The enclosing <> are optional, so you don't\nhave to use them - in fact the first thing globmap will do is remove\nthese delimiters if they are present.\n\nThe first parameter to \"globmap\", \"*.tar.gz\", is an Input File Glob.\nOnce the enclosing \"< ... >\" is removed, this is passed (more or less)\nunchanged to \"File::Glob\" to carry out a file match.\n\nNext the fileglob \"*.tar.gz\" is transformed behind the scenes into a\nfull Perl regular expression, with the additional step of wrapping each\ntransformed wildcard metacharacter sequence in parenthesis.\n\nIn this case the input fileglob \"*.tar.gz\" will be transformed into\nthis Perl regular expression\n\n([^/]*)\\.tar\\.gz\n\nWrapping with parenthesis allows the wildcard parts of the Input File\nGlob to be referenced by the second parameter to \"globmap\", \"#1.tgz\",\nthe Output File Glob. This parameter operates just like the replacement\npart of a substitute command. The difference is that the \"#1\" syntax is\nused to reference sub-patterns matched in the input fileglob, rather\nthan the $1 syntax that is used with perl regular expressions. In this\ncase \"#1\" is used to refer to the text matched by the \"*\" in the Input\nFile Glob. This makes it easier to use this module where the parameters\nto \"globmap\" are typed at the command line.\n\nThe final step involves passing each filename matched by the \"*.tar.gz\"\nfile glob through the derived Perl regular expression in turn and\nexpanding the output fileglob using it.\n\nThe end result of all this is a list of pairs of filenames. By default\nthat is what is returned by \"globmap\". In this example the data\nstructure returned will look like this\n\n( ['alpha.tar.gz' => 'alpha.tgz'],\n['beta.tar.gz'  => 'beta.tgz' ],\n['gamma.tar.gz' => 'gamma.tgz']\n)\n\nEach pair is an array reference with two elements - namely the from\nfilename, that \"File::Glob\" has matched, and a to filename that is\nderived from the from filename.\n\nLimitations\n\"File::GlobMapper\" has been kept simple deliberately, so it isn't\nintended to solve all filename mapping operations. Under the hood\n\"File::Glob\" (or for older versions of Perl, \"File::BSDGlob\") is used\nto match the files, so you will never have the flexibility of full Perl\nregular expression.\n\nInput File Glob\nThe syntax for an Input FileGlob is identical to \"File::Glob\", except\nfor the following\n\n1.   No nested {}\n\n2.   Whitespace does not delimit fileglobs.\n\n3.   The use of parenthesis can be used to capture parts of the input\nfilename.\n\n4.   If an Input glob matches the same file more than once, only the\nfirst will be used.\n\nThe syntax\n\n~\n~user\n.    Matches a literal '.'.  Equivalent to the Perl regular expression\n\n\\.\n\n*    Matches zero or more characters, except '/'. Equivalent to the\nPerl regular expression\n\n[^/]*\n\n?    Matches zero or one character, except '/'. Equivalent to the Perl\nregular expression\n\n[^/]?\n\n\\    Backslash is used, as usual, to escape the next character.\n\n[]   Character class.\n\n{,}  Alternation\n\n()   Capturing parenthesis that work just like perl\n\nAny other character it taken literally.\n\nOutput File Glob\nThe Output File Glob is a normal string, with 2 glob-like features.\n\nThe first is the '*' metacharacter. This will be replaced by the\ncomplete filename matched by the input file glob. So\n\n*.c *.Z\n\nThe second is\n\nOutput FileGlobs take the\n\n\"*\"  The \"*\" character will be replaced with the complete input\nfilename.\n\n#1   Patterns of the form /#\\d/ will be replaced with the\n\nReturned Data",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "A Rename script\nBelow is a simple \"rename\" script that uses \"globmap\" to determine the\nsource and destination filenames.\n\nuse File::GlobMapper qw(globmap) ;\nuse File::Copy;\n\ndie \"rename: Usage rename 'from' 'to'\\n\"\nunless @ARGV == 2 ;\n\nmy $fromGlob = shift @ARGV;\nmy $toGlob   = shift @ARGV;\n\nmy $pairs = globmap($fromGlob, $toGlob)\nor die $File::GlobMapper::Error;\n\nfor my $pair (@$pairs)\n{\nmy ($from, $to) = @$pair;\nmove $from => $to ;\n}\n\nHere is an example that renames all c files to cpp.\n\n$ rename '*.c' '#1.cpp'\n\nA few example globmaps\nBelow are a few examples of globmaps\n\nTo copy all your .c file to a backup directory\n\n'</my/home/*.c>'    '</my/backup/#1.c>'\n\nIf you want to compress all\n\n'</my/home/*.[ch]>'    '<*.gz>'\n\nTo uncompress\n\n'</my/home/*.[ch].gz>'    '</my/home/#1.#2>'\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "File::Glob\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "The File::GlobMapper module was written by Paul Marquess,\npmqs@cpan.org.\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "Copyright (c) 2005 Paul Marquess. All rights reserved.  This program is\nfree software; you can redistribute it and/or modify it under the same\nterms as Perl itself.\n\nperl v5.34.0                      2026-06-23           File::GlobMapper(3perl)",
            "subsections": []
        }
    },
    "summary": "File::GlobMapper - Extend File Glob to Allow Input and Output Files",
    "flags": [],
    "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 is 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": []
}