{
    "mode": "perldoc",
    "parameter": "Template::Plugin::File",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Template%3A%3APlugin%3A%3AFile/json",
    "generated": "2026-10-06T13:21:51Z",
    "synopsis": "[% USE File(filepath) %]\n[% File.path %]         # full path\n[% File.name %]         # filename\n[% File.dir %]          # directory",
    "sections": {
        "NAME": {
            "content": "Template::Plugin::File - Plugin providing information about files\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "[% USE File(filepath) %]\n[% File.path %]         # full path\n[% File.name %]         # filename\n[% File.dir %]          # directory\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This plugin provides an abstraction of a file. It can be used to fetch details about files from\nthe file system, or to represent abstract files (e.g. when creating an index page) that may or\nmay not exist on a file system.\n\nA file name or path should be specified as a constructor argument. e.g.\n\n[% USE File('foo.html') %]\n[% USE File('foo/bar/baz.html') %]\n[% USE File('/foo/bar/baz.html') %]\n\nThe file should exist on the current file system (unless \"nostat\" option set, see below) as an\nabsolute file when specified with as leading '\"/\"' as per '\"/foo/bar/baz.html\"', or otherwise as\none relative to the current working directory. The constructor performs a stat() on the file and\nmakes the 13 elements returned available as the plugin items:\n\ndev ino mode nlink uid gid rdev size\natime mtime ctime blksize blocks\n\ne.g.\n\n[% USE File('/foo/bar/baz.html') %]\n\n[% File.mtime %]\n[% File.mode %]\n...\n\nIn addition, the \"user\" and \"group\" items are set to contain the user and group names as\nreturned by calls to getpwuid() and getgrgid() for the file \"uid\" and \"gid\" elements,\nrespectively. On Win32 platforms on which getpwuid() and getgrid() are not available, these\nvalues are undefined.\n\n[% USE File('/tmp/foo.html') %]\n[% File.uid %]      # e.g. 500\n[% File.user %]     # e.g. abw\n\nThis user/group lookup can be disabled by setting the \"noid\" option.\n\n[% USE File('/tmp/foo.html', noid=1) %]\n[% File.uid %]      # e.g. 500\n[% File.user %]     # nothing\n\nThe \"isdir\" flag will be set if the file is a directory.\n\n[% USE File('/tmp') %]\n[% File.isdir %]    # 1\n\nIf the stat() on the file fails (e.g. file doesn't exists, bad permission, etc) then the\nconstructor will throw a \"File\" exception. This can be caught within a \"TRY...CATCH\" block.\n\n[% TRY %]\n[% USE File('/tmp/myfile') %]\nFile exists!\n[% CATCH File %]\nFile error: [% error.info %]\n[% END %]\n\nNote the capitalisation of the exception type, '\"File\"', to indicate an error thrown by the\n\"File\" plugin, to distinguish it from a regular \"file\" exception thrown by the Template Toolkit.\n\nNote that the \"File\" plugin can also be referenced by the lower case name '\"file\"'. However,\nexceptions are always thrown of the \"File\" type, regardless of the capitalisation of the plugin\nnamed used.\n\n[% USE file('foo.html') %]\n[% file.mtime %]\n\nAs with any other Template Toolkit plugin, an alternate name can be specified for the object\ncreated.\n\n[% USE foo = file('foo.html') %]\n[% foo.mtime %]\n\nThe \"nostat\" option can be specified to prevent the plugin constructor from performing a stat()\non the file specified. In this case, the file does not have to exist in the file system, no\nattempt will be made to verify that it does, and no error will be thrown if it doesn't. The\nentries for the items usually returned by stat() will be set empty.\n\n[% USE file('/some/where/over/the/rainbow.html', nostat=1)\n[% file.mtime %]     # nothing\n",
            "subsections": []
        },
        "METHODS": {
            "content": "All \"File\" plugins, regardless of the \"nostat\" option, have set a number of items relating to\nthe original path specified.\n\npath\nThe full, original file path specified to the constructor.\n\n[% USE file('/foo/bar.html') %]\n[% file.path %]     # /foo/bar.html\n\nname\nThe name of the file without any leading directories.\n\n[% USE file('/foo/bar.html') %]\n[% file.name %]     # bar.html\n\ndir\nThe directory element of the path with the filename removed.\n\n[% USE file('/foo/bar.html') %]\n[% file.name %]     # /foo\n\next\nThe file extension, if any, appearing at the end of the path following a '\".\"' (not included in\nthe extension).\n\n[% USE file('/foo/bar.html') %]\n[% file.ext %]      # html\n\nhome\nThis contains a string of the form '\"../..\"' to represent the upward path from a file to its\nroot directory.\n\n[% USE file('bar.html') %]\n[% file.home %]     # nothing\n\n[% USE file('foo/bar.html') %]\n[% file.home %]     # ..\n\n[% USE file('foo/bar/baz.html') %]\n[% file.home %]     # ../..\n\nroot\nThe \"root\" item can be specified as a constructor argument, indicating a root directory in which\nthe named file resides. This is otherwise set empty.\n\n[% USE file('foo/bar.html', root='/tmp') %]\n[% file.root %]     # /tmp\n\nabs\nThis returns the absolute file path by constructing a path from the \"root\" and \"path\" options.\n\n[% USE file('foo/bar.html', root='/tmp') %]\n[% file.path %]     # foo/bar.html\n[% file.root %]     # /tmp\n[% file.abs %]      # /tmp/foo/bar.html\n\nrel(path)\nThis returns a relative path from the current file to another path specified as an argument. It\nis constructed by appending the path to the '\"home\"' item.\n\n[% USE file('foo/bar/baz.html') %]\n[% file.rel('wiz/waz.html') %]      # ../../wiz/waz.html\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "[% USE file('/foo/bar/baz.html') %]\n\n[% file.path  %]      # /foo/bar/baz.html\n[% file.dir   %]      # /foo/bar\n[% file.name  %]      # baz.html\n[% file.home  %]      # ../..\n[% file.root  %]      # ''\n[% file.abs   %]      # /foo/bar/baz.html\n[% file.ext   %]      # html\n[% file.mtime %]      # 987654321\n[% file.atime %]      # 987654321\n[% file.uid   %]      # 500\n[% file.user  %]      # abw\n\n[% USE file('foo.html') %]\n\n[% file.path %]           # foo.html\n[% file.dir  %]       # ''\n[% file.name %]           # foo.html\n[% file.root %]       # ''\n[% file.home %]       # ''\n[% file.abs  %]       # foo.html\n\n[% USE file('foo/bar/baz.html') %]\n\n[% file.path %]           # foo/bar/baz.html\n[% file.dir  %]       # foo/bar\n[% file.name %]           # baz.html\n[% file.root %]       # ''\n[% file.home %]       # ../..\n[% file.abs  %]       # foo/bar/baz.html\n\n[% USE file('foo/bar/baz.html', root='/tmp') %]\n\n[% file.path %]           # foo/bar/baz.html\n[% file.dir  %]       # foo/bar\n[% file.name %]           # baz.html\n[% file.root %]       # /tmp\n[% file.home %]       # ../..\n[% file.abs  %]       # /tmp/foo/bar/baz.html\n\n# calculate other file paths relative to this file and its root\n[% USE file('foo/bar/baz.html', root => '/tmp/tt2') %]\n\n[% file.path('baz/qux.html') %]         # ../../baz/qux.html\n[% file.dir('wiz/woz.html')  %]     # ../../wiz/woz.html\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Michael Stevens wrote the original \"Directory\" plugin on which this is based. Andy Wardley split\nit into separate \"File\" and \"Directory\" plugins, added some extra code and documentation for\n\"VIEW\" support, and made a few other minor tweaks.\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright 2000-2007 Michael Stevens, Andy Wardley.\n\nThis module is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Template::Plugin, Template::Plugin::Directory, Template::View\n",
            "subsections": []
        }
    },
    "summary": "Template::Plugin::File - Plugin providing information about files",
    "flags": [],
    "examples": [
        "[% USE file('/foo/bar/baz.html') %]",
        "[% file.path  %]      # /foo/bar/baz.html",
        "[% file.dir   %]      # /foo/bar",
        "[% file.name  %]      # baz.html",
        "[% file.home  %]      # ../..",
        "[% file.root  %]      # ''",
        "[% file.abs   %]      # /foo/bar/baz.html",
        "[% file.ext   %]      # html",
        "[% file.mtime %]      # 987654321",
        "[% file.atime %]      # 987654321",
        "[% file.uid   %]      # 500",
        "[% file.user  %]      # abw",
        "[% USE file('foo.html') %]",
        "[% file.path %]           # foo.html",
        "[% file.dir  %]       # ''",
        "[% file.name %]           # foo.html",
        "[% file.root %]       # ''",
        "[% file.home %]       # ''",
        "[% file.abs  %]       # foo.html",
        "[% USE file('foo/bar/baz.html') %]",
        "[% file.path %]           # foo/bar/baz.html",
        "[% file.dir  %]       # foo/bar",
        "[% file.name %]           # baz.html",
        "[% file.root %]       # ''",
        "[% file.home %]       # ../..",
        "[% file.abs  %]       # foo/bar/baz.html",
        "[% USE file('foo/bar/baz.html', root='/tmp') %]",
        "[% file.path %]           # foo/bar/baz.html",
        "[% file.dir  %]       # foo/bar",
        "[% file.name %]           # baz.html",
        "[% file.root %]       # /tmp",
        "[% file.home %]       # ../..",
        "[% file.abs  %]       # /tmp/foo/bar/baz.html",
        "# calculate other file paths relative to this file and its root",
        "[% USE file('foo/bar/baz.html', root => '/tmp/tt2') %]",
        "[% file.path('baz/qux.html') %]         # ../../baz/qux.html",
        "[% file.dir('wiz/woz.html')  %]     # ../../wiz/woz.html"
    ],
    "see_also": []
}