Skip to content

Instantly share code, notes, and snippets.

@jberger
Created January 16, 2012 20:01
Show Gist options
  • Save jberger/1622668 to your computer and use it in GitHub Desktop.
Save jberger/1622668 to your computer and use it in GitHub Desktop.
A possible PDL::SampleData implementation using File::ShareDir
package PDL::SampleData;
use strict;
use warnings;
use Carp;
use PDL;
use File::ShareDir 'module_dir';
use File::chdir;
sub get {
my $filename = shift;
local $CWD = module_dir 'PDL::SampleData';
unless ( -e $filename ) {
carp "Could not find file $filename in $CWD";
return 0;
}
return rfits $filename;
}
sub available {
local $CWD = module_dir 'PDL::SampleData';
my @valid_extensions = qw/fits/;
opendir( my $dh, $CWD);
my @files;
while (my $filename = readdir $dh) {
push @files, $filename
if grep { $filename =~ /\.$_$/i } @valid_extensions;
}
return @files;
}
1;
__END__
Makefile.PL will need lines
use File::ShareDir::Install;
install_share module => 'My::Module' => 'sample-data';
where m51.fits is in the folder sample-data relative to the Makefile.PL
Dependencies for File::ShareDir and File::ShareDir::Install will need to be added
__POD__
=head1 NAME
PDL::SampleData - Easy access to data used in the examples and the L<PDL::Book>.
=head1 SYNOPSIS
use PDL::ShareDir;
my $pdl = PDL::ShareDir::get('m51.fits');
=head1 DESCRIPTION
C<PDL::SampleData> allows easy access to the data needed to follow along with the examples and the L<PDL::Book>.
=head1 FUNCTIONS
Nothing is exported, use the fully qualified name to access the function.
=head2 PDL::SampleData::get( filename )
Takes the name (with extension) of the file requested. On success it returns the data as a L<PDL> object. On failure a warning is issued and a false value is returned.
=head2 PDL::SampleData::available( filename )
Takes no arguments, returns the sample data files that are available by using C<get>.
=head1 AUTHOR
Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2012 by Joel Berger
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment