How to include data in a Perl distribution

If you are a CPAN author or use the CPAN-style tools to distribute your data, you may have wondered how to include data with your Perl distribution.

Any file under lib in the distribution is bundled into the distribution by Makefile.PL. So if your distribution is called Acme::Include::Data, then a file called lib/Acme/Include/this-is-a-data-file.txt is automatically included into the distribution.

To read the data file in, simply use __FILE__:

my $data = __FILE__;

# Make whatever substitutions are necessary:

$data =~ s/Data\.pm$/this-is-a-data-file.txt/;

# Read the data in:

open my $in, "<", $data or die $!;
my $text = '';
while (<$in>) {
    $text .= $_;

Some examples on CPAN include Lingua::JA::Moji, where all the data files used by the module live in a subdirectory and are read in in this way.

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer