{
    "content": [
        {
            "type": "text",
            "text": "# File::GlobMapper (perldoc)\n\n## NAME\n\nFile::GlobMapper - Extend File Glob to Allow Input and Output Files\n\n## SYNOPSIS\n\nuse 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 ;\n\n## DESCRIPTION\n\nThis module needs Perl5.005 or better.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION** (5 subsections)\n- **EXAMPLES**\n- **SEE ALSO**\n- **AUTHOR**\n- **COPYRIGHT AND LICENSE**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "File::GlobMapper",
        "section": "",
        "mode": "perldoc",
        "summary": "File::GlobMapper - Extend File Glob to Allow Input and Output Files",
        "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 ;",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "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": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 8,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 10,
                "subsections": [
                    {
                        "name": "Behind The Scenes",
                        "lines": 89
                    },
                    {
                        "name": "Limitations",
                        "lines": 5
                    },
                    {
                        "name": "Input File Glob",
                        "lines": 36
                    },
                    {
                        "name": "Output File Glob",
                        "lines": 15
                    },
                    {
                        "name": "Returned Data",
                        "lines": 1
                    }
                ]
            },
            {
                "name": "EXAMPLES",
                "lines": 41,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT AND LICENSE",
                "lines": 3,
                "subsections": []
            }
        ],
        "sections": {
            "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 and extends it to allow\nnew filenames to be derived from the files matched by \"File::Glob\".\n\nThis can be useful when carrying out batch operations on multiple files that have both an input\nfilename and output filename and the output file can be derived from the input filename.\nExamples of operations where this can be useful include, file renaming, file copying and file\ncompression.\n",
                "subsections": [
                    {
                        "name": "Behind The Scenes",
                        "content": "To help explain what \"File::GlobMapper\" does, consider what code you would write if you wanted\nto rename all files in the current directory that ended in \".tar.gz\" to \".tgz\". So say these\nfiles are in the current 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 (error cases have been\nomitted)\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 \".tar.gz\" files, then a fairly\nsimilar regular expression was used in the substitute to allow the new filename to be created.\n\nGiven that the file glob is just a cut-down regular expression and that it has already done a\nlot of the hard work in pattern matching the filenames, wouldn't it be handy to be able to use\nthe patterns in the fileglob 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 glob to match existing\nfilenames followed by a substitute to create the new filenames.\n\nNotice how both parameters to \"globmap\" are strings that are delimited by <>. This is done to\nmake them look more like file globs - it is just syntactic sugar, but it can be handy when you\nwant the strings to be visually distinctive. The enclosing <> are optional, so you don't have to\nuse them - in fact the first thing globmap will do is remove these delimiters if they are\npresent.\n\nThe first parameter to \"globmap\", \"*.tar.gz\", is an *Input File Glob*. Once the enclosing \"< ...\n>\" is removed, this is passed (more or less) unchanged to \"File::Glob\" to carry out a file\nmatch.\n\nNext the fileglob \"*.tar.gz\" is transformed behind the scenes into a full Perl regular\nexpression, with the additional step of wrapping each transformed wildcard metacharacter\nsequence in parenthesis.\n\nIn this case the input fileglob \"*.tar.gz\" will be transformed into this Perl regular expression\n\n([^/]*)\\.tar\\.gz\n\nWrapping with parenthesis allows the wildcard parts of the Input File Glob to be referenced by\nthe second parameter to \"globmap\", \"#1.tgz\", the *Output File Glob*. This parameter operates\njust like the replacement part of a substitute command. The difference is that the \"#1\" syntax\nis used to reference sub-patterns matched in the input fileglob, rather than the $1 syntax that\nis used with perl regular expressions. In this case \"#1\" is used to refer to the text matched by\nthe \"*\" in the Input File Glob. This makes it easier to use this module where the parameters to\n\"globmap\" are typed at the command line.\n\nThe final step involves passing each filename matched by the \"*.tar.gz\" file glob through the\nderived Perl regular expression in turn and expanding the output fileglob using it.\n\nThe end result of all this is a list of pairs of filenames. By default that is what is returned\nby \"globmap\". In this example the data structure 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* filename, that\n\"File::Glob\" has matched, and a *to* filename that is derived from the *from* filename.\n"
                    },
                    {
                        "name": "Limitations",
                        "content": "\"File::GlobMapper\" has been kept simple deliberately, so it isn't intended to solve all filename\nmapping operations. Under the hood \"File::Glob\" (or for older versions of Perl, \"File::BSDGlob\")\nis used to match the files, so you will never have the flexibility of full Perl regular\nexpression.\n"
                    },
                    {
                        "name": "Input File Glob",
                        "content": "The syntax for an Input FileGlob is identical to \"File::Glob\", except for 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 filename.\n\n4.   If an Input glob matches the same file more than once, only the first 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 Perl regular expression\n\n[^/]*\n\n?    Matches zero or one character, except '/'. Equivalent to the Perl regular 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"
                    },
                    {
                        "name": "Output File Glob",
                        "content": "The Output File Glob is a normal string, with 2 glob-like features.\n\nThe first is the '*' metacharacter. This will be replaced by the complete filename matched by\nthe 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 filename.\n\n#1   Patterns of the form /#\\d/ will be replaced with the\n"
                    },
                    {
                        "name": "Returned Data",
                        "content": ""
                    }
                ]
            },
            "EXAMPLES": {
                "content": "A Rename script\nBelow is a simple \"rename\" script that uses \"globmap\" to determine the source and destination\nfilenames.\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, pmqs@cpan.org.\n",
                "subsections": []
            },
            "COPYRIGHT AND LICENSE": {
                "content": "Copyright (c) 2005 Paul Marquess. All rights reserved. This program is free software; you can\nredistribute it and/or modify it under the same terms as Perl itself.\n",
                "subsections": []
            }
        }
    }
}