Bio-Gonzales
view release on metacpan or search on metacpan
lib/Bio/Gonzales/Util/File.pm view on Meta::CPAN
package Bio::Gonzales::Util::File;
use warnings;
use strict;
use Carp;
use File::Spec;
use Scalar::Util;
use IO::Handle;
use IO::Zlib;
use IO::Uncompress::Bunzip2 qw($Bunzip2Error);
use File::Which qw/which/;
use Bio::Gonzales::Util::IO::Compressed;
our %ZMODES = (
'>' => 'wb',
'>>' => 'ab',
'<', => 'rb',
);
use base 'Exporter';
our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
our $VERSION = '0.0546'; # VERSION
our $EXTERNAL_GZ = which('pigz') // which('gzip');
our $EXTERNAL_BZIP2 = which('bzip2');
@EXPORT = qw(glob_regex epath bname openod spath);
%EXPORT_TAGS = ();
@EXPORT_OK
= qw(expand_path slurpc basename regex_glob open_on_demand is_newer splitpath %ZMODES is_archive expand_home);
sub epath { expand_path(@_) }
sub expand_path {
my @files = @_;
my @expanded;
for my $file (@files) {
push @expanded, File::Spec->rel2abs( expand_home($file) );
}
return wantarray ? @expanded : ( shift @expanded );
}
sub expand_home {
my $file = shift;
$file =~ s{ ^ ~ ( [^/]* ) }
{ $1
? (getpwnam($1))[7]
: ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>))[7] )
}ex;
return $file;
}
sub regex_glob { return glob_regex(@_) }
sub glob_regex {
my ( $dir, $re ) = @_;
$dir = expand_path($dir);
opendir( my $dh, $dir ) || die "can't opendir >$dir< $!";
my @res;
for ( readdir($dh) ) {
push @res, File::Spec->catfile( $dir, $_ ) if ( /$re/ && $_ !~ /^\.\.?$/ );
}
closedir $dh;
return wantarray ? @res : \@res;
}
sub slurpc {
my ( $fh, $was_open ) = open_on_demand( $_[0], '<' );
my @lines = map { s/\r\n/\n/; chomp; $_ } <$fh>;
$fh->close if ( !$was_open );
return wantarray ? @lines : \@lines;
}
sub bname { return basename(@_) }
sub basename {
my $f = shift;
my ( $dir, $base ) = ( File::Spec->splitpath($f) )[ 1, 2 ];
$base =~ s/\.([^.]*?)$//;
my $suffix = $1;
return wantarray ? ( $dir, $base, $suffix ) : $base;
}
sub spath { return splitpath(@_) }
sub splitpath {
my $f = shift;
my ( $dir, $filename ) = ( File::Spec->splitpath($f) )[ 1, 2 ];
$dir =~ s![\/\\]$!!;
return ( $dir, $filename );
}
sub openod { return open_on_demand(@_) }
sub open_on_demand {
my ( $src, $mode ) = @_;
confess "no file or filehandle given" unless ($src);
confess "no file open mode given or mode not known: $mode" unless ( $mode && exists( $ZMODES{$mode} ) );
my $fh;
my $fh_was_open;
( run in 0.930 second using v1.01-cache-2.11-cpan-0b5f733616e )