Bio::Root::Utilities - General-purpose utility module


Object Creation

    use Bio::Root::Utilities qw(:obj);

There is no need to create a new object when the :obj tag is used. This tag will import the static $Util object created by into your name space. This saves you from having to call new Bio::Root::Utilities.

You are free to not use the :obj tag and create the object as you like, but a Bio::Root::Utilities object is not configurable; any given script only needs a single copy.

    $date_stamp = $Util->date_format('yyy-mm-dd');

    $clean = $Util->untaint($dirty);

    $Util->mail_authority("Something you should know about...");

    ...and other methods. See below.


This module is included with the central Bioperl distribution:

Follow the installation instructions included in the README file.


Provides general-purpose utilities of potential interest to any Perl script. Scripts and modules are expected to use the static $Util object exported by this package with the :obj tag.

DEPENDENCIES inherits from It also relies on the GNU gzip program for file compression/uncompression.

SEE ALSO       - Core object       - Manages global variables/constants  - Online module documentation                       - Bioperl Project Homepage (included in the Perl distribution or CPAN).


Mailing Lists

User feedback is an integral part of the evolution of this and other Bioperl modules. Send your comments and suggestions preferably to one of the Bioperl mailing lists. Your participation is much appreciated.             - General discussion - About the mailing lists

Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track the bugs and their resolution. Bug reports can be submitted via email or the web:                         


Steve Chervitz <>

See the FEEDBACK section for where to send bug reports and comments.

VERSION, 0.042


This module was developed under the auspices of the Saccharomyces Genome Database:


Copyright (c) 1997-98 Steve Chervitz. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Methods beginning with a leading underscore are considered private and are intended for internal use by this module. They are not considered part of the public interface and are described here for documentation purposes only.


 Title     : date_format
 Usage     : $Util->date_format( [FMT], [DATE]) 
 Purpose   : -- Get a string containing the formated date or time
           :    taken when this routine is invoked.
           : -- Provides a way to avoid using `date`.
           : -- Provides an interface to localtime().
           : -- Interconverts some date formats.
           : (For additional functionality, use Date::Manip or
           :  Date::DateCalc available from CPAN).
 Example   : $Util->date_format();
           : $date = $Util->date_format('yyyy-mmm-dd', '11/22/92');
 Returns   : String (unless 'list' is provided as argument, see below)
           :   'yyyy-mm-dd'  = 1996-05-03    # default format. 
           :   'yyyy-dd-mm'  = 1996-03-05   
           :   'yyyy-mmm-dd' = 1996-May-03
           :   'd-m-y'       = 3-May-1996
           :   'd m y'       = 3 May 1996
           :   'dmy'         = 3may96
           :   'mdy'         = May 3, 1996
           :   'ymd'         = 96may3
           :   'md'          = may3
           :   'year'        = 1996
           :   'hms'         = 23:01:59  # 'hms' can be tacked on to any of the above options
           :                             # to add the time stamp: eg 'dmyhms'
           :   'full' | 'unix' = UNIX-style date: Tue May  5 22:00:00 1998
           :   'list'          = the contents of localtime(time) in an array.
 Argument  : (all are optional)
           : FMT  = yyyy-mm-dd | yyyy-dd-mm | yyyy-mmm-dd |
           :        mdy | ymd | md | d-m-y | hms | hm 
           :        ('hms' may be appended to any of these to 
           :        add a time stamp)
           : DATE = String containing date to be converted.
           :        Acceptable input formats:
           :           12/1/97 (for 1 December 1997)
           :           1997-12-01 
           :           1997-Dec-01
 Throws    : 
 Comments  : Relies on the $BASE_YEAR constant exported by
           : If you don't care about formatting or using backticks, you can
           : always use: $date = `date`;
           : For more features, use, (which I should 
           : probably switch to...)

See Also : file_date(), month2num()


 Title      : month2num
 Purpose    : Converts a string containing a name of a month to integer
            : representing the number of the month in the year.
 Example    : $Util->month2num("march");  # returns 3
 Argument   : The string argument must contain at least the first
            : three characters of the month's name. Case insensitive.
 Throws     : Exception if the conversion fails.


 Title   : num2month
 Purpose : Does the opposite of month2num.
         : Converts a number into a string containing a name of a month.
 Example : $Util->num2month(3);  # returns 'Mar'
 Throws  : Exception if supplied number is out of range.


 Title     : compress
 Usage     : $Util->compress(filename, [tmp]);
 Purpose   : Compress a file to conserve disk space.
 Example   : $Util->compress("/usr/people/me/data.txt");
 Returns   : String (name of compressed file, full path).
 Argument  : filename = String (name of file to be compressed, full path).
           :            If the supplied filename ends with '.gz' or '.Z',
           :            that extension will be removed before attempting to compress.
           : tmp = boolean, 
           :    If true, (or if user is not the owner of the file)
           :         the file is compressed to a tmp file
           :    If false, file is clobbered with the compressed version.
 Throws    : Exception if file cannot be compressed
           : If user is not owner of the file, generates a warning
           :   and compresses to a tmp file.
           :   To avoid this warning, use the -o file test operator
           :   and call this function with a true second argument.
 Comments  : Attempts to compress using gzip (default compression level).
           : If that fails, will attempt to use compress.
           : In some situations, the full path to the gzip executable
           : may be required. This can be specified with the $GNU_PATH
           : package global variable. When installed, $GNU_PATH is an
           : empty string.

See Also : uncompress()


 Title     : uncompress
 Usage     : $Util->uncompress(filename, [tmp]);
 Purpose   : Uncompress a file.
 Example   : $Util->uncompress("/usr/people/me/data.txt.gz");
 Returns   : String (name of uncompressed file, full path).
 Argument  : filename = String (name of file to be uncompressed, full path).
           :           If the supplied filename does not end with '.gz' or '.Z'
           :           a '.gz' will be appended before attempting to uncompress.
           : tmp = boolean, 
           :    If true, (or if user is not the owner of the file)
           :         the file is uncompressed to a tmp file
           :    If false, file is clobbered with the uncompressed version.
 Throws    : Exception if file cannot be uncompressed
           : If user is not owner of the file, generates a warning
           :   and uncompresses to a tmp file.
           :   To avoid this warning, use the -o file test operator
           :   and call this function with a true second argument.
 Comments  : Attempts to uncompress using gunzip.
           : If that fails, will use uncompress.
           : In some situations, the full path to the gzip executable
           : may be required. This can be specified with the $GNU_PATH
           : package global variable. When installed, $GNU_PATH is an
           : empty string.

See Also : compress()


 Title    : file_date
 Usage    : $Util->file_date( filename [,date_format])
 Purpose  : Obtains the date of a given file.
          : Provides flexible formatting via date_format().
 Returns  : String = date of the file as: yyyy-mm-dd (e.g., 1997-10-15)
 Argument : filename = string, full path name for file
          : date_format = string, desired format for date (see date_format()).
          :               Default = yyyy-mm-dd
 Thows    : Exception if no file is provided or does not exist.
 Comments : Uses the mtime field as obtained by stat().


 Title   : untaint
 Purpose : To remove nasty shell characters from untrusted data
         : and allow a script to run with the -T switch.
         : Potentially dangerous shell meta characters:  &;`'\"|*?!~<>^()[]{}$\n\r
         : Accept only the first block of contiguous characters:
         :  Default allowed chars = "-\w.', ()"
         :  If $relax is true  = "-\w.', ()\/=%:^<>*"
 Usage   : $Util->untaint($value, $relax)
 Returns : String containing the untained data.
 Argument: $value = string 
         : $relax = boolean
     This general untaint() function may not be appropriate for every situation.
     To allow only a more restricted subset of special characters 
     (for example, untainting a regular expression), then using a custom 
     untainting mechanism would permit more control.

     Note that special trusted vars (like $0) require untainting.


 Title    : mean_stdev
 Usage    : ($mean, $stdev) = $Util->mean_stdev( @data )
 Purpose  : Calculates the mean and standard deviation given a list of numbers.
 Returns  : 2-element list (mean, stdev)
 Argument : list of numbers (ints or floats)
 Thows    : n/a


 Title    : count_files
 Purpose  : Counts the number of files/directories within a given directory.
          : Also reports the number of text and binary files in the dir
          : as well as names of these files and directories.
 Usage    : count_files(\%data)
          :   $data{-DIR} is the directory to be analyzed. Default is ./
          :   $data{-PRINT} = 0|1; if 1, prints results to STDOUT, (default=0).
 Argument : Hash reference (empty)
 Returns  : n/a;
          : Modifies the hash ref passed in as the sole argument.
          :  $$href{-TOTAL}            scalar
          :  $$href{-NUM_TEXT_FILES}   scalar
          :  $$href{-NUM_BINARY_FILES} scalar
          :  $$href{-NUM_DIRS}         scalar
          :  $$href{-T_FILE_NAMES}     array ref
          :  $$href{-B_FILE_NAMES}     array ref
          :  $$href{-DIRNAMES}         array ref


 Usage     : $object->create_filehandle(<named parameters>);
 Purpose   : Create a FileHandle object from a file or STDIN.
           : Mainly used as a helper method by read() and get_newline().
 Example   : $data = $object->create_filehandle(-FILE =>'usr/people/me/data.txt')
 Argument  : Named parameters (case-insensitive):
           :  (all optional)
           :    -CLIENT  => object reference for the object submitting
           :                the request. This facilitates use by
           :                Bio::Root::IOManager::read(). Default = $Util.
           :    -FILE    => string (full path to file) or a reference
           :                to a FileHandle object or typeglob. This is an
           :                optional parameter (if not defined, STDIN is used).
 Returns   : Reference to a FileHandle object.   
 Throws    : Exception if cannot open a supplied file or if supplied with a
           : reference that is not a FileHandle ref.
 Comments  : If given a FileHandle reference, this method simply returns it.
           : This method assumes the user wants to read ascii data. So, if
           : the file is binary, it will be treated as a compressed (gzipped)
           : file and access it using gzip -ce. The problem here is that not
           : all binary files are necessarily compressed. Therefore, 
           : this method should probably have a -mode parameter to
           : specify ascii or binary.

See Also : get_newline(), Bio::Root::IOManager::read(),


 Usage     : $object->get_newline(<named parameters>);
 Purpose   : Determine the character(s) used for newlines in a given file or
           : input stream. Delegates to Bio::Root::Utilities::get_newline()
 Example   : $data = $object->get_newline(-CLIENT => $anObj,
           :                                   -FILE =>'usr/people/me/data.txt')
 Argument  : Same arguemnts as for create_filehandle().
 Returns   : Reference to a FileHandle object.   
 Throws    : Propogates and exceptions thrown by Bio::Root::Utilities::get_newline().

See Also : taste_file(), create_filehandle()


 Usage     : $object->taste_file( <FileHandle> );
           : Mainly a utility method for get_newline().
 Purpose   : Sample a filehandle to determine the character(s) used for a newline.
 Example   : $char = $Util->taste_file($FH)
 Argument  : Reference to a FileHandle object.
 Returns   : String containing an octal represenation of the newline character string.
           :   Unix = "\012"  ("\n")
           :   Win32 = "\012\015" ("\r\n")
           :   Mac = "\015"  ("\r")
 Throws    : Exception if no input is read within $TIMEOUT_SECS seconds.
           : Exception if argument is not FileHandle object reference.
           : Warning if cannot determine neewline char(s).
 Comments  : Based on code submitted by Vicki Brown (

See Also : get_newline()


 Title    : mail_authority
 Usage    : $Util->mail_authority( $message )
 Purpose  : Syntactic sugar to send email to $Bio::Root::Global::AUTHORITY

See Also : send_mail()


 Title    : send_mail
 Usage    : $Util->send_mail( named_parameters )
 Purpose  : Provides an interface to /usr/lib/sendmail  
 Returns  : n/a
 Argument : Named parameters:  (case-insensitive)
          :  -TO   => e-mail address to send to
          :  -SUBJ => subject for message  (optional)
          :  -MSG  => message to be sent   (optional)
          :  -CC   => cc: e-mail address   (optional)
 Thows    : Exception if TO: address appears bad or is missing
 Comments : Based on  TomC's tip at:
          : Using default 'From:' information.
          :   sendmail options used:
          :      -t: ignore the address given on the command line and 
          :          get To:address from the e-mail header.
          :     -oi: prevents send_mail from ending the message if it 
          :          finds a period at the start of a line.

See Also : mail_authority()


 Title   : yes_reply()
 Usage   : $Util->yes_reply( [query_string]);
 Purpose : To test an STDIN input value for affirmation.
 Example : print +( $Util->yes_reply('Are you ok') ? "great!\n" : "sorry.\n" );
         : $Util->yes_reply('Continue') || die;
 Returns : Boolean, true (1) if input string begins with 'y' or 'Y' 
 Argument: query_string = string to be used to prompt user (optional)
         : If not provided, 'Yes or no' will be used.
         : Question mark is automatically appended.


 Title   : request_data()
 Usage   : $Util->request_data( [value_name]);
 Purpose : To request data from a user to be entered via keyboard (STDIN).
 Example : $name = $Util->request_data('Name');
         : # User will see: % Enter Name: 
 Returns : String, (data entered from keyboard, sans terminal newline.)
 Argument: value_name = string to be used to prompt user.
         : If not provided, 'data' will be used, (not very helpful).
         : Question mark is automatically appended.


 Purpose : Checks the version of Perl used to invoke the script.
         : Aborts program if version is less than the given argument.
 Usage   : verify_version('5.000')