# $Id: exceptions_enabled.pm,v 1.4 1999/09/23 21:26:30 v2kea318 Exp $
# $Log: exceptions_enabled.pm,v $
# Revision 1.4  1999/09/23  21:26:30  v2kea318
# Added POD format documentation.  Make the $@ local to the try routine.
#
# Revision 1.3  1999/04/05  15:05:46  v2kea318
# Add a newline character to the string being thrown.  This will eliminate
# the extra text Perl adds to the string, specifying the line number at
# which die was called.
#
# Revision 1.2  1998/12/08  22:32:14  v2kea318
# Added a package name and set the @EXPORT list to export the 3 function
# names.
#
# Revision 1.1  1998/12/08  22:30:37  v2kea318
# Initial revision
#
#***************************************************************************

=head1 NAME

exceptions_enabled - define syntax to mimic C++ exception handling

=head1 DESCRIPTION

This module defines the try, catch, and throw syntax to mimic the C++
exception handling scheme.  Exceptions are strings, which is stored in
$_ in the catch block.  A semi-colon must follow the final } of the catch
block.

=head1 EXAMPLE PROGRAM

  use exceptions_enabled;
  try {
    throw "phooey";
  } catch {
    /phooey/ && print "unphooey\n";
  };

=cut

#***************************************************************************

# Package name.
package exceptions_enabled;
use Exporter ();
@ISA = qw(Exporter);
@EXPORT = qw(try catch throw);

# Prototype the try, catch, and throw functions.

sub try (&@);
sub catch(&);
sub throw($);

# implement the functions.

sub try (&@) {
  local $@;
  my($try,$catch) = @_;
  eval { &$try };
  if ($@) {
    local $_ = $@;
    &$catch;
  }
}

#***************************************************************************

sub catch (&) { $_[0] }

#***************************************************************************

sub throw($) {
  my ($s) = @_;
  die $s . "\n";
}

1;



