[%# # IMPORTANT NOTE # This documentation is generated automatically from source # templates. Any changes you make here may be lost. # # The 'docsrc' documentation source bundle is available for download # from http://www.template-toolkit.org/docs.html and contains all # the source templates, XML files, scripts, etc., from which the # documentation for the Template Toolkit is built. -%] [% META book = 'Modules' page = 'Plugin_File' %] [% WRAPPER toc; PROCESS tocitem title ="SYNOPSIS" subs = []; PROCESS tocitem title ="DESCRIPTION" subs = []; PROCESS tocitem title ="EXAMPLES" subs = []; PROCESS tocitem title ="AUTHORS" subs = []; PROCESS tocitem title ="VERSION" subs = []; PROCESS tocitem title ="COPYRIGHT" subs = []; PROCESS tocitem title ="SEE ALSO" subs = []; END %] [% WRAPPER section title="SYNOPSIS" -%]

        [% tt_start_tag %] USE File(filepath) [% tt_end_tag %]
        [% tt_start_tag %] File.path [% tt_end_tag %]         # full path
        [% tt_start_tag %] File.name [% tt_end_tag %]	    # filename
        [% tt_start_tag %] File.dir [% tt_end_tag %]          # directory

[%- END %] [% WRAPPER section title="DESCRIPTION" -%]

This plugin provides an abstraction of a file. It can be used to fetch details about files from the file system, or to represent abstract files (e.g. when creating an index page) that may or may not exist on a file system.

A file name or path should be specified as a constructor argument. e.g.

        [% tt_start_tag %] USE File('foo.html') [% tt_end_tag %]
        [% tt_start_tag %] USE File('foo/bar/baz.html') [% tt_end_tag %]
        [% tt_start_tag %] USE File('/foo/bar/baz.html') [% tt_end_tag %]

The file should exist on the current file system (unless 'nostat' option set, see below) as an absolute file when specified with as leading '/' as per '/foo/bar/baz.html', or otherwise as one relative to the current working directory. The constructor performs a stat() on the file and makes the 13 elements returned available as the plugin items:

        dev ino mode nlink uid gid rdev size
        atime mtime ctime blksize blocks

e.g.

        [% tt_start_tag %] USE File('/foo/bar/baz.html') [% tt_end_tag %]

        [% tt_start_tag %] File.mtime [% tt_end_tag %]
        [% tt_start_tag %] File.mode [% tt_end_tag %]
        ...

In addition, the 'user' and 'group' items are set to contain the user and group names as returned by calls to getpwuid() and getgrgid() for the file 'uid' and 'gid' elements, respectively. On Win32 platforms on which getpwuid() and getgrid() are not available, these values are undefined.

        [% tt_start_tag %] USE File('/tmp/foo.html') [% tt_end_tag %]
        [% tt_start_tag %] File.uid [% tt_end_tag %]	# e.g. 500
        [% tt_start_tag %] File.user [% tt_end_tag %]     # e.g. abw

This user/group lookup can be disabled by setting the 'noid' option.

        [% tt_start_tag %] USE File('/tmp/foo.html', noid=1) [% tt_end_tag %]
        [% tt_start_tag %] File.uid [% tt_end_tag %]	# e.g. 500
        [% tt_start_tag %] File.user [% tt_end_tag %]     # nothing

The 'isdir' flag will be set if the file is a directory.

        [% tt_start_tag %] USE File('/tmp') [% tt_end_tag %]
        [% tt_start_tag %] File.isdir [% tt_end_tag %]	# 1

If the stat() on the file fails (e.g. file doesn't exists, bad permission, etc) then the constructor will throw a 'File' exception. This can be caught within a TRY...CATCH block.

        [% tt_start_tag %] TRY [% tt_end_tag %]
           [% tt_start_tag %] USE File('/tmp/myfile') [% tt_end_tag %]
           File exists!
        [% tt_start_tag %] CATCH File [% tt_end_tag %]
           File error: [% tt_start_tag %] error.info [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

Note the capitalisation of the exception type, 'File' to indicate an error thrown by the 'File' plugin, to distinguish it from a regular 'file' exception thrown by the Template Toolkit.

Note that the 'File' plugin can also be referenced by the lower case name 'file'. However, exceptions are always thrown of the 'File' type, regardless of the capitalisation of the plugin named used.

        [% tt_start_tag %] USE file('foo.html') [% tt_end_tag %]
        [% tt_start_tag %] file.mtime [% tt_end_tag %]

As with any other Template Toolkit plugin, an alternate name can be specified for the object created.

        [% tt_start_tag %] USE foo = file('foo.html') [% tt_end_tag %]
        [% tt_start_tag %] foo.mtime [% tt_end_tag %]

The 'nostat' option can be specified to prevent the plugin constructor from performing a stat() on the file specified. In this case, the file does not have to exist in the file system, no attempt will be made to verify that it does, and no error will be thrown if it doesn't. The entries for the items usually returned by stat() will be set empty.

        [% tt_start_tag %] USE file('/some/where/over/the/rainbow.html', nostat=1)
        [% tt_start_tag %] file.mtime [% tt_end_tag %]     # nothing

All File plugins, regardless of the nostat option, have set a number of items relating to the original path specified.

- path

  The full, original file path specified to the constructor.

          [% tt_start_tag %] USE file('/foo/bar.html') [% tt_end_tag %]
          [% tt_start_tag %] file.path [% tt_end_tag %]	# /foo/bar.html

- name

  The name of the file without any leading directories.

          [% tt_start_tag %] USE file('/foo/bar.html') [% tt_end_tag %]
          [% tt_start_tag %] file.name [% tt_end_tag %]	# bar.html

- dir

  The directory element of the path with the filename removed.

          [% tt_start_tag %] USE file('/foo/bar.html') [% tt_end_tag %]
          [% tt_start_tag %] file.name [% tt_end_tag %]	# /foo

- ext

  The file extension, if any, appearing at the end of the path following a '.' (not included in the extension).

          [% tt_start_tag %] USE file('/foo/bar.html') [% tt_end_tag %]
          [% tt_start_tag %] file.ext [% tt_end_tag %]	# html

- home

  This contains a string of the form '../..' to represent the upward path from a file to its root directory.

          [% tt_start_tag %] USE file('bar.html') [% tt_end_tag %]
          [% tt_start_tag %] file.home [% tt_end_tag %]	# nothing

          [% tt_start_tag %] USE file('foo/bar.html') [% tt_end_tag %]
          [% tt_start_tag %] file.home [% tt_end_tag %]	# ..

          [% tt_start_tag %] USE file('foo/bar/baz.html') [% tt_end_tag %]
          [% tt_start_tag %] file.home [% tt_end_tag %]	# ../..

- root

  The 'root' item can be specified as a constructor argument, indicating a root directory in which the named file resides. This is otherwise set empty.

          [% tt_start_tag %] USE file('foo/bar.html', root='/tmp') [% tt_end_tag %]
          [% tt_start_tag %] file.root [% tt_end_tag %]	# /tmp

- abs

  This returns the absolute file path by constructing a path from the 'root' and 'path' options.

          [% tt_start_tag %] USE file('foo/bar.html', root='/tmp') [% tt_end_tag %]
          [% tt_start_tag %] file.path [% tt_end_tag %]	# foo/bar.html
          [% tt_start_tag %] file.root [% tt_end_tag %]	# /tmp
          [% tt_start_tag %] file.abs [% tt_end_tag %]	# /tmp/foo/bar.html

In addition, the following method is provided:

- rel(path)

  This returns a relative path from the current file to another path specified as an argument. It is constructed by appending the path to the 'home' item.

          [% tt_start_tag %] USE file('foo/bar/baz.html') [% tt_end_tag %]
          [% tt_start_tag %] file.rel('wiz/waz.html') [% tt_end_tag %]	# ../../wiz/waz.html

[%- END %] [% WRAPPER section title="EXAMPLES" -%]

        [% tt_start_tag %] USE file('/foo/bar/baz.html') [% tt_end_tag %]

        [% tt_start_tag %] file.path  [% tt_end_tag %]      # /foo/bar/baz.html
        [% tt_start_tag %] file.dir   [% tt_end_tag %]      # /foo/bar
        [% tt_start_tag %] file.name  [% tt_end_tag %]      # baz.html
        [% tt_start_tag %] file.home  [% tt_end_tag %]      # ../..
        [% tt_start_tag %] file.root  [% tt_end_tag %]      # ''
        [% tt_start_tag %] file.abs   [% tt_end_tag %]      # /foo/bar/baz.html
        [% tt_start_tag %] file.ext   [% tt_end_tag %]      # html
        [% tt_start_tag %] file.mtime [% tt_end_tag %]	  # 987654321
        [% tt_start_tag %] file.atime [% tt_end_tag %]      # 987654321
        [% tt_start_tag %] file.uid   [% tt_end_tag %]      # 500
        [% tt_start_tag %] file.user  [% tt_end_tag %]      # abw

        [% tt_start_tag %] USE file('foo.html') [% tt_end_tag %]

        [% tt_start_tag %] file.path [% tt_end_tag %]	      # foo.html
        [% tt_start_tag %] file.dir  [% tt_end_tag %]       # ''
        [% tt_start_tag %] file.name [% tt_end_tag %]	      # foo.html
        [% tt_start_tag %] file.root [% tt_end_tag %]       # ''
        [% tt_start_tag %] file.home [% tt_end_tag %]       # ''
        [% tt_start_tag %] file.abs  [% tt_end_tag %]       # foo.html

        [% tt_start_tag %] USE file('foo/bar/baz.html') [% tt_end_tag %]

        [% tt_start_tag %] file.path [% tt_end_tag %]	      # foo/bar/baz.html
        [% tt_start_tag %] file.dir  [% tt_end_tag %]       # foo/bar
        [% tt_start_tag %] file.name [% tt_end_tag %]	      # baz.html
        [% tt_start_tag %] file.root [% tt_end_tag %]       # ''
        [% tt_start_tag %] file.home [% tt_end_tag %]       # ../..
        [% tt_start_tag %] file.abs  [% tt_end_tag %]       # foo/bar/baz.html

        [% tt_start_tag %] USE file('foo/bar/baz.html', root='/tmp') [% tt_end_tag %]

        [% tt_start_tag %] file.path [% tt_end_tag %]	      # foo/bar/baz.html
        [% tt_start_tag %] file.dir  [% tt_end_tag %]       # foo/bar
        [% tt_start_tag %] file.name [% tt_end_tag %]	      # baz.html
        [% tt_start_tag %] file.root [% tt_end_tag %]       # /tmp
        [% tt_start_tag %] file.home [% tt_end_tag %]       # ../..
        [% tt_start_tag %] file.abs  [% tt_end_tag %]       # /tmp/foo/bar/baz.html

        # calculate other file paths relative to this file and its root
        [% tt_start_tag %] USE file('foo/bar/baz.html', root => '/tmp/tt2') [% tt_end_tag %]

        [% tt_start_tag %] file.path('baz/qux.html') [% tt_end_tag %]	    # ../../baz/qux.html
        [% tt_start_tag %] file.dir('wiz/woz.html')  [% tt_end_tag %]     # ../../wiz/woz.html

[%- END %] [% WRAPPER section title="AUTHORS" -%]

Michael Stevens <michael@etla.org> wrote the original Directory plugin on which this is based. Andy Wardley <abw@wardley.org> split it into separate File and Directory plugins, added some extra code and documentation for VIEW support, and made a few other minor tweaks.

[%- END %] [% WRAPPER section title="VERSION" -%]

2.71, distributed as part of the Template Toolkit version 2.19, released on 27 April 2007.

[%- END %] [% WRAPPER section title="COPYRIGHT" -%]

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

[%- END %] [% WRAPPER section title="SEE ALSO" -%]

[% ttlink('Template::Plugin', 'Template::Plugin') -%], [% ttlink('Template::Plugin::Directory', 'Template::Plugin::Directory') -%], [% ttlink('Template::View', 'Template::View') -%]

[%- END %]