Summary | Included libraries | Package variables | Synopsis | Description | General documentation | Methods |
WebCvs | Raw content |
# any bioperl or bioperl compliant object is a RootI
# compliant object
# Here's how to throw and catch an exception using the eval-based syntax. $obj->throw("This is an exception"); eval { $obj->throw("This is catching an exception"); }; if( $@ ) { print "Caught exception"; } else { print "no exception"; } # Alternatively, using the new typed exception syntax in the throw() call: $obj->throw( -class => 'Bio::Root::BadParameter', -text => "Can't open file $file", -value => $file); # Exceptions can be used in an eval{} block as shown above or within # a try{} block if you have installed the Error.pm module. # Here's a brief example. For more, see Bio::Root::Exception use Error qw(:try); try { $obj->throw( # arguments as above ); } catch Bio::Root::FileOpenException with { my $err = shift; print "Handling exception $err\n"; };
try {
open( IN, $file) || $obj->throw( -class => 'Bio::Root::FileOpenException',
-text => "Cannot open file $file for reading",
-value => $!);
}
catch Bio::Root::BadParameter with {
my $err = shift; # get the Error object
# Perform specific exception handling code for the FileOpenException
}
catch Bio::Root::Exception with {
my $err = shift; # get the Error object
# Perform general exception handling code for any Bioperl exception.
}
otherwise {
# A catch-all for any other type of exception
}
finally {
# Any code that you want to execute regardless of whether or not
# an exception occurred.
};
# the ending semicolon is essential!
BEGIN | Code | |
DESTROY | No description | Code |
_cleanup_methods | No description | Code |
_load_module | Description | Code |
_register_for_cleanup | No description | Code |
_unregister_for_cleanup | No description | Code |
debug | Description | Code |
new | Description | Code |
throw | Description | Code |
verbose | Description | Code |
_load_module | code | next | Top |
Title : _load_module |
debug | code | prev | next | Top |
Title : debug |
new | code | prev | next | Top |
Purpose : generic instantiation function can be overridden if |
throw | code | prev | next | Top |
Title : throw |
verbose | code | prev | next | Top |
Title : verbose |
BEGIN | Top |
$ID = 'Bio::Root::Root'; $VERSION = 1.0; $Revision = '$Id: Root.pm,v 1.30 2002/12/16 09:44:28 birney Exp $ '; $DEBUG = 0; $VERBOSITY = 0; $ERRORLOADED = 0; # Check whether or not Error.pm is available.}
# $main::DONT_USE_ERROR is intended for testing purposes and also
# when you don't want to use the Error module, even if it is installed.
# Just put a INIT { $DONT_USE_ERROR = 1; } at the top of your script.
if( not $main::DONT_USE_ERROR ) { if ( eval "require Error" ) { import Error qw(:try); require Bio::Root::Exception; $ERRORLOADED = 1; $Error::Debug = 1; # enable verbose stack trace
} } if( !$ERRORLOADED ) { require Carp; import Carp qw( confess ); } $main::DONT_USE_ERROR; # so that perl -w won't warn "used only once"
DESTROY | description | prev | next | Top |
my $self = shift; my @cleanup_methods = $self->_cleanup_methods or return; for my $method (@cleanup_methods) { $method->($self); } } 1;}
_cleanup_methods | description | prev | next | Top |
my $self = shift; return unless ref $self && $self->isa('HASH'); my $methods = $self->{'_root_cleanup_methods'} or return; @$methods;}
_load_module | description | prev | next | Top |
my ($self, $name) = @_; my ($module, $load, $m); $module = "_<$name.pm"; return 1 if $main::{$module}; # untaint operation for safe web-based running (modified after a fix}
# a fix by Lincoln) HL
if ($name !~ /^([\w:]+)$/) { $self->throw("$name is an illegal perl package name"); } $load = "$name.pm"; my $io = Bio::Root::IO->new(); # catfile comes from IO
$load = $io->catfile((split(/::/,$load))); eval { require $load; }; if ( $@ ) { $self->throw("Failed to load module $name. ".$@); } return 1;
_register_for_cleanup | description | prev | next | Top |
my ($self,$method) = @_; if($method) { if(! exists($self->{'_root_cleanup_methods'})) { $self->{'_root_cleanup_methods'} = []; } push(@{$self->{'_root_cleanup_methods'}},$method); }}
_unregister_for_cleanup | description | prev | next | Top |
my ($self,$method) = @_; my @methods = grep {$_ ne $method} $self->_cleanup_methods; $self->{'_root_cleanup_methods'} =\@ methods;}
debug | description | prev | next | Top |
my ($self,@msgs) = @_; if( $self->verbose > 0 ) { print STDERR join("", @msgs); }}
new | description | prev | next | Top |
# my ($class, %param) = @_;}
my $class = shift; my $self = {}; bless $self, ref($class) || $class; if(@_ > 1) { # if the number of arguments is odd but at least 3, we'll give
# it a try to find -verbose
shift if @_ % 2; my %param = @_; ## See "Comments" above regarding use of _rearrange().
$self->verbose($param{'-VERBOSE'} || $param{'-verbose'}); } return $self;
throw | description | prev | next | Top |
my ($self,@args) = @_; my ( $text, $class ) = $self->_rearrange( [qw(TEXT CLASS)], @args); if( $ERRORLOADED ) { # print STDERR " Calling Error::throw\n\n";}
# Enable re-throwing of Error objects.
# If the error is not derived from Bio::Root::Exception,
# we can't guarantee that the Error's value was set properly
# and, ipso facto, that it will be catchable from an eval{}.
# But chances are, if you're re-throwing non-Bio::Root::Exceptions,
# you're probably using Error::try(), not eval{}.
# TODO: Fix the MSG: line of the re-thrown error. Has an extra line
# containing the '----- EXCEPTION -----' banner.
if( ref($args[0])) { if( $args[0]->isa('Error')) { my $class = ref $args[0]; throw $class ( @args ); } else { my $text .= "\nWARNING: Attempt to throw a non-Error.pm object: " . ref$args[0]; my $class = "Bio::Root::Exception"; throw $class ( '-text' => $text, '-value' => $args[0] ); } } else { $class ||= "Bio::Root::Exception"; my %args; if( @args % 2 == 0 && $args[0] =~ /^-/ ) { %args = @args; $args{-text} = $text; $args{-object} = $self; } throw $class ( scalar keys %args > 0 ? %args : @args ); # (%args || @args) puts %args in scalar context!
} } else { # print STDERR " Not calling Error::throw\n\n";
$class ||= ''; my $std = $self->stack_trace_dump(); my $title = "------------- EXCEPTION $class -------------"; my $footer = "\n" . '-' x CORE::length($title); $text ||= ''; my $out = "\n$title\n" . "MSG: $text\n". $std . $footer . "\n"; die $out; }
verbose | description | prev | next | Top |
my ($self,$value) = @_; # allow one to set global verbosity flag}
return $DEBUG if $DEBUG; return $VERBOSITY unless ref $self; if (defined $value || ! defined $self->{'_root_verbose'}) { $self->{'_root_verbose'} = $value || 0; } return $self->{'_root_verbose'};
CONTACT | Top |
APPENDIX | Top |