File-Utils

 view release on metacpan or  search on metacpan

lib/File/Utils.pm  view on Meta::CPAN

package File::Utils;
use Modern::Perl;
use Carp;
use PerlIO::gzip;
use Exporter;

our $VERSION = '0.0.5'; # VERSION
# ABSTRACT: Provide various of file operation

our @ISA = ('Exporter');
our @EXPORT = ();
our @EXPORT_OK = qw/read_handle write_handle file2array/;
our %EXPORT_TAGS = (
  gz    => [qw/read_handle write_handle/],
  data  => [qw/file2array/]
);



sub read_handle {
  my $file = shift;
  my $handle;
  if ($file =~/\.gz$/) {
    open $handle, "<:gzip", $file;
  } else {
    open $handle, "<", $file;
  }
  $handle->autoflush;
  return $handle;
}


sub write_handle {
  my $file = shift;
  my $handle;
  if ($file =~/\.gz$/) {
    open $handle, ">:gzip", $file;
  } else {
    open $handle, ">", $file;
  }
  $handle->autoflush;
  return $handle;
}


sub file2array {
  my ($filename, $sep) = @_;
  $sep ||= "\t";
  open my $file, "<", "$filename";
  my @out;
  while (my $line = <$file>) {
    chomp($line);
    my @cols = split $sep, $line;
    push @out, \@cols;
  }
  close($file);
  return @out;
}

1;

__END__



( run in 1.123 second using v1.01-cache-2.11-cpan-437f7b0c052 )