Archive Ensembl HomeArchive Ensembl Home
Bio::EnsEMBL::Utils::IO Class Reference

List of all members.


Class Summary

Synopsis

    use Bio::EnsEMBL::Utils::IO qw/slurp work_with_file slurp_to_array fh_to_array/;
    #or
    # use Bio::EnsEMBL::Utils::IO qw/:slurp/; #brings in any method starting with slurp
    # use Bio::EnsEMBL::Utils::IO qw/:array/; #brings in any method which ends with _array
    # use Bio::EnsEMBL::Utils::IO qw/:gz/;    #brings all methods which start with gz_
    # use Bio::EnsEMBL::Utils::IO qw/:all/;   #brings all methods in
    
    #As a scalar
  my $file_contents = slurp('/my/file/location.txt');
  print length($file_contents);
  
  #As a ref
  my $file_contents_ref = slurp('/my/file/location.txt', 1);
  print length($$file_contents_ref);
  
  #Sending it to an array
  my $array = slurp_to_array('/my/location');
  work_with_file('/my/location', 'r', sub {
    $array = process_to_array($_[0], sub {
      #Gives us input line by line
      return "INPUT: $_";
    });
  });
  
  #Simplified vesion but without the post processing
  $array = fh_to_array($fh);
  
  #Sending this back out to another file
  work_with_file('/my/file/newlocation.txt', 'w', sub {
    my ($fh) = @_;
    print $fh $$file_contents_ref;
    return;
  });
  
  #Gzipping the data to another file
  gz_work_with_file('/my/file.gz', 'w', sub {
    my ($fh) = @_;
    print $fh $$file_contents_ref;
    return;
  });

Description

A collection of subroutines aimed to helping IO based operations
 

Definition at line 57 of file IO.pm.

Available Methods

public ArrayRef fh_to_array ()
public Scalar gz_slurp ()
public ArrayRef gz_slurp_to_array ()
public void gz_work_with_file ()
public process_to_array ()
public Scalar slurp ()
public ArrayRef slurp_to_array ()
public void work_with_file ()

Method Documentation

public ArrayRef Bio::EnsEMBL::Utils::IO::fh_to_array ( )
  Arg [1]     : Glob/IO::Handle $fh
  Arg [2]     : boolean $chomp
  Description : Sends the contents of the given filehandle into an ArrayRef. 
                Will perform chomp on each line if specified. If you require
                any more advanced line based processing then see 
                process_to_array.
  Returntype  : ArrayRef
  Example     : my $contents_array = fh_to_array($fh);
  Exceptions  : None
  Status      : Stable
 
Code:
click to view
public Scalar Bio::EnsEMBL::Utils::IO::gz_slurp ( )
  Arg [1]     : string $file
  Arg [2]     : boolean; $want_ref
  Arg [3]     : boolean; $binary
                Indicates if we want to return a scalar reference
  Description : Forces the contents of a file into a scalar. This is the 
                fastest way to get a file into memory in Perl. You can also
                get a scalar reference back to avoid copying the file contents
                in Scalar references. If the input file is binary then specify
                with the binary flag
  Returntype  : Scalar or reference of the file contents depending on arg 2
  Example     : my $contents = slurp('/tmp/file.txt.gz');
  Exceptions  : If the file did not exist or was not readable
  Status      : Stable
 
Code:
click to view
public ArrayRef Bio::EnsEMBL::Utils::IO::gz_slurp_to_array ( )
  Arg [1]     : string $file
  Arg [2]     : boolean $chomp
  Description : Sends the contents of the given gzipped file into an ArrayRef
  Returntype  : ArrayRef
  Example     : my $contents_array = slurp_to_array('/tmp/file.txt.gz');
  Exceptions  : If the file did not exist or was not readable
  Status      : Stable
 
Code:
click to view
public void Bio::EnsEMBL::Utils::IO::gz_work_with_file ( )
  Arg [1]     : string $file
  Arg [2]     : string; $mode 
                Supports modes like r, w, \> and \<
  Arg [3]     : CodeRef the callback which is given the open file handle as
                its only argument
  Description : Performs the nitty gritty of checking if a file handle is open
                and closing the resulting filehandle down.
  Returntype  : None
  Example     : work_with_file('/tmp/out.txt.gz', 'w', sub { 
                  my ($fh) = @_; 
                  print $fh 'hello'; 
                  return;
                });
  Exceptions  : If we could not work with the file due to permissions
  Status      : Stable
 
Code:
click to view
public Bio::EnsEMBL::Utils::IO::process_to_array ( )

Undocumented method

Code:
click to view
public Scalar Bio::EnsEMBL::Utils::IO::slurp ( )
  Arg [1]     : string $file
  Arg [2]     : boolean; $want_ref
  Arg [3]     : boolean; $binary
                Indicates if we want to return a scalar reference
  Description : Forces the contents of a file into a scalar. This is the 
                fastest way to get a file into memory in Perl. You can also
                get a scalar reference back to avoid copying the file contents
                in Scalar references. If the input file is binary then specify
                with the binary flag
  Returntype  : Scalar or reference of the file contents depending on arg 2
  Example     : my $contents = slurp('/tmp/file.txt');
  Exceptions  : If the file did not exist or was not readable
  Status      : Stable
 
Code:
click to view
public ArrayRef Bio::EnsEMBL::Utils::IO::slurp_to_array ( )
  Arg [1]     : string $file
  Arg [2]     : boolean $chomp
  Description : Sends the contents of the given file into an ArrayRef
  Returntype  : ArrayRef
  Example     : my $contents_array = slurp_to_array('/tmp/file.txt');
  Exceptions  : If the file did not exist or was not readable
  Status      : Stable
 
Code:
click to view
public void Bio::EnsEMBL::Utils::IO::work_with_file ( )
  Arg [1]     : string $file
  Arg [2]     : string; $mode 
                Supports all modes specified by the open() function as well as those 
                supported by IO::File
  Arg [3]     : CodeRef the callback which is given the open file handle as
                its only argument
  Description : Performs the nitty gritty of checking if a file handle is open
                and closing the resulting filehandle down.
  Returntype  : None
  Example     : work_with_file('/tmp/out.txt', 'w', sub { 
                  my ($fh) = @_; 
                  print $fh 'hello'; 
                  return;
                });
  Exceptions  : If we could not work with the file due to permissions
  Status      : Stable
 
Code:
click to view

The documentation for this class was generated from the following file:
  • Bio/EnsEMBL/Utils/IO.pm