.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "File::ShareDir 3" .TH File::ShareDir 3 2024-09-01 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME File::ShareDir \- Locate per\-dist and per\-module shared files .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use File::ShareDir \*(Aq:ALL\*(Aq; \& \& # Where are distribution\-level shared data files kept \& $dir = dist_dir(\*(AqFile\-ShareDir\*(Aq); \& \& # Where are module\-level shared data files kept \& $dir = module_dir(\*(AqFile::ShareDir\*(Aq); \& \& # Find a specific file in our dist/module shared dir \& $file = dist_file( \*(AqFile\-ShareDir\*(Aq, \*(Aqfile/name.txt\*(Aq); \& $file = module_file(\*(AqFile::ShareDir\*(Aq, \*(Aqfile/name.txt\*(Aq); \& \& # Like module_file, but search up the inheritance tree \& $file = class_file( \*(AqFoo::Bar\*(Aq, \*(Aqfile/name.txt\*(Aq ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" The intent of File::ShareDir is to provide a companion to Class::Inspector and File::HomeDir, modules that take a process that is well-known by advanced Perl developers but gets a little tricky, and make it more available to the larger Perl community. .PP Quite often you want or need your Perl module (CPAN or otherwise) to have access to a large amount of read-only data that is stored on the file-system at run-time. .PP On a linux-like system, this would be in a place such as /usr/share, however Perl runs on a wide variety of different systems, and so the use of any one location is unreliable. .PP Perl provides a little-known method for doing this, but almost nobody is aware that it exists. As a result, module authors often go through some very strange ways to make the data available to their code. .PP The most common of these is to dump the data out to an enormous Perl data structure and save it into the module itself. The result are enormous multi-megabyte .pm files that chew up a lot of memory needlessly. .PP Another method is to put the data "file" after the _\|_DATA_\|_ compiler tag and limit yourself to access as a filehandle. .PP The problem to solve is really quite simple. .PP .Vb 1 \& 1. Write the data files to the system at install time. \& \& 2. Know where you put them at run\-time. .Ve .PP Perl's install system creates an "auto" directory for both every distribution and for every module file. .PP These are used by a couple of different auto-loading systems to store code fragments generated at install time, and various other modules written by the Perl "ancient masters". .PP But the same mechanism is available to any dist or module to store any sort of data. .SS "Using Data in your Module" .IX Subsection "Using Data in your Module" \&\f(CW\*(C`File::ShareDir\*(C'\fR forms one half of a two part solution. .PP Once the files have been installed to the correct directory, you can use \f(CW\*(C`File::ShareDir\*(C'\fR to find your files again after the installation. .PP For the installation half of the solution, see File::ShareDir::Install and its \f(CW\*(C`install_share\*(C'\fR directive. .PP Using File::ShareDir::Install together with File::ShareDir allows one to rely on the files in appropriate \f(CWdist_dir()\fR or \f(CWmodule_dir()\fR in development phase, too. .SH FUNCTIONS .IX Header "FUNCTIONS" \&\f(CW\*(C`File::ShareDir\*(C'\fR provides four functions for locating files and directories. .PP For greater maintainability, none of these are exported by default and you are expected to name the ones you want at use-time, or provide the \f(CW\*(Aq:ALL\*(Aq\fR tag. All of the following are equivalent. .PP .Vb 3 \& # Load but don\*(Aqt import, and then call directly \& use File::ShareDir; \& $dir = File::ShareDir::dist_dir(\*(AqMy\-Dist\*(Aq); \& \& # Import a single function \& use File::ShareDir \*(Aqdist_dir\*(Aq; \& dist_dir(\*(AqMy\-Dist\*(Aq); \& \& # Import all the functions \& use File::ShareDir \*(Aq:ALL\*(Aq; \& dist_dir(\*(AqMy\-Dist\*(Aq); .Ve .PP All of the functions will check for you that the dir/file actually exists, and that you have read permissions, or they will throw an exception. .SS dist_dir .IX Subsection "dist_dir" .Vb 2 \& # Get a distribution\*(Aqs shared files directory \& my $dir = dist_dir(\*(AqMy\-Distribution\*(Aq); .Ve .PP The \f(CW\*(C`dist_dir\*(C'\fR function takes a single parameter of the name of an installed (CPAN or otherwise) distribution, and locates the shared data directory created at install time for it. .PP Returns the directory path as a string, or dies if it cannot be located or is not readable. .SS module_dir .IX Subsection "module_dir" .Vb 2 \& # Get a module\*(Aqs shared files directory \& my $dir = module_dir(\*(AqMy::Module\*(Aq); .Ve .PP The \f(CW\*(C`module_dir\*(C'\fR function takes a single parameter of the name of an installed (CPAN or otherwise) module, and locates the shared data directory created at install time for it. .PP In order to find the directory, the module \fBmust\fR be loaded when calling this function. .PP Returns the directory path as a string, or dies if it cannot be located or is not readable. .SS dist_file .IX Subsection "dist_file" .Vb 2 \& # Find a file in our distribution shared dir \& my $dir = dist_file(\*(AqMy\-Distribution\*(Aq, \*(Aqfile/name.txt\*(Aq); .Ve .PP The \f(CW\*(C`dist_file\*(C'\fR function takes two parameters of the distribution name and file name, locates the dist directory, and then finds the file within it, verifying that the file actually exists, and that it is readable. .PP The filename should be a relative path in the format of your local filesystem. It will simply added to the directory using File::Spec's \&\f(CW\*(C`catfile\*(C'\fR method. .PP Returns the file path as a string, or dies if the file or the dist's directory cannot be located, or the file is not readable. .SS module_file .IX Subsection "module_file" .Vb 2 \& # Find a file in our module shared dir \& my $dir = module_file(\*(AqMy::Module\*(Aq, \*(Aqfile/name.txt\*(Aq); .Ve .PP The \f(CW\*(C`module_file\*(C'\fR function takes two parameters of the module name and file name. It locates the module directory, and then finds the file within it, verifying that the file actually exists, and that it is readable. .PP In order to find the directory, the module \fBmust\fR be loaded when calling this function. .PP The filename should be a relative path in the format of your local filesystem. It will simply added to the directory using File::Spec's \&\f(CW\*(C`catfile\*(C'\fR method. .PP Returns the file path as a string, or dies if the file or the dist's directory cannot be located, or the file is not readable. .SS class_file .IX Subsection "class_file" .Vb 2 \& # Find a file in our module shared dir, or in our parent class \& my $dir = class_file(\*(AqMy::Module\*(Aq, \*(Aqfile/name.txt\*(Aq); .Ve .PP The \f(CW\*(C`module_file\*(C'\fR function takes two parameters of the module name and file name. It locates the module directory, and then finds the file within it, verifying that the file actually exists, and that it is readable. .PP In order to find the directory, the module \fBmust\fR be loaded when calling this function. .PP The filename should be a relative path in the format of your local filesystem. It will simply added to the directory using File::Spec's \&\f(CW\*(C`catfile\*(C'\fR method. .PP If the file is NOT found for that module, \f(CW\*(C`class_file\*(C'\fR will scan up the module's \f(CW@ISA\fR tree, looking for the file in all of the parent classes. .PP This allows you to, in effect, "subclass" shared files. .PP Returns the file path as a string, or dies if the file or the dist's directory cannot be located, or the file is not readable. .SH EXTENDING .IX Header "EXTENDING" .SS "Overriding Directory Resolution" .IX Subsection "Overriding Directory Resolution" \&\f(CW\*(C`File::ShareDir\*(C'\fR has two convenience hashes for people who have advanced usage requirements of \f(CW\*(C`File::ShareDir\*(C'\fR such as using uninstalled \f(CW\*(C`share\*(C'\fR directories during development. .PP .Vb 4 \& # \& # Dist\-Name => /absolute/path/for/DistName/share/dir \& # \& %File::ShareDir::DIST_SHARE \& \& # \& # Module::Name => /absolute/path/for/Module/Name/share/dir \& # \& %File::ShareDir::MODULE_SHARE .Ve .PP Setting these values any time before the corresponding calls .PP .Vb 2 \& dist_dir(\*(AqDist\-Name\*(Aq) \& dist_file(\*(AqDist\-Name\*(Aq,\*(Aqsome/file\*(Aq); \& \& module_dir(\*(AqModule::Name\*(Aq); \& module_file(\*(AqModule::Name\*(Aq,\*(Aqsome/file\*(Aq); .Ve .PP Will override the base directory for resolving those calls. .PP An example of where this would be useful is in a test for a module that depends on files installed into a share directory, to enable the tests to use the development copy without needing to install them first. .PP .Vb 3 \& use File::ShareDir; \& use Cwd qw( getcwd ); \& use File::Spec::Functions qw( rel2abs catdir ); \& \& $File::ShareDir::MODULE_SHARE{\*(AqFoo::Module\*(Aq} = rel2abs(catfile(getcwd,\*(Aqshare\*(Aq)); \& \& use Foo::Module; \& \& # internal calls in Foo::Module to module_file(\*(AqFoo::Module\*(Aq,\*(Aqbar\*(Aq) now resolves to \& # the source trees share/ directory instead of something in @INC .Ve .SH SUPPORT .IX Header "SUPPORT" Bugs should always be submitted via the CPAN request tracker, see below. .PP You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc File::ShareDir .Ve .PP You can also look for information at: .IP \(bu 4 RT: CPAN's request tracker .Sp .IP \(bu 4 AnnoCPAN: Annotated CPAN documentation .Sp .IP \(bu 4 CPAN Ratings .Sp .IP \(bu 4 CPAN Search .Sp .SS "Where can I go for other help?" .IX Subsection "Where can I go for other help?" If you have a bug report, a patch or a suggestion, please open a new report ticket at CPAN (but please check previous reports first in case your issue has already been addressed). .PP Report tickets should contain a detailed description of the bug or enhancement request and at least an easily verifiable way of reproducing the issue or fix. Patches are always welcome, too. .SS "Where can I go for help with a concrete version?" .IX Subsection "Where can I go for help with a concrete version?" Bugs and feature requests are accepted against the latest version only. To get patches for earlier versions, you need to get an agreement with a developer of your choice \- who may or not report the issue and a suggested fix upstream (depends on the license you have chosen). .SS "Business support and maintenance" .IX Subsection "Business support and maintenance" For business support you can contact the maintainer via his CPAN email address. Please keep in mind that business support is neither available for free nor are you eligible to receive any support based on the license distributed with this package. .SH AUTHOR .IX Header "AUTHOR" Adam Kennedy .SS MAINTAINER .IX Subsection "MAINTAINER" Jens Rehsack .SH "SEE ALSO" .IX Header "SEE ALSO" File::ShareDir::Install, File::ConfigDir, File::HomeDir, Module::Install, Module::Install::Share, File::ShareDir::PAR, Dist::Zilla::Plugin::ShareDir .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright 2005 \- 2011 Adam Kennedy, Copyright 2014 \- 2018 Jens Rehsack. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP The full text of the license can be found in the LICENSE file included with this module.