| 1 | package ARB; |
|---|
| 2 | |
|---|
| 3 | my $already_dying = 0; |
|---|
| 4 | my $in_global_eval = 1; |
|---|
| 5 | |
|---|
| 6 | use strict; |
|---|
| 7 | use vars qw($VERSION @ISA @EXPORT); |
|---|
| 8 | |
|---|
| 9 | require Exporter; |
|---|
| 10 | require DynaLoader; |
|---|
| 11 | |
|---|
| 12 | @ISA = qw(Exporter DynaLoader); |
|---|
| 13 | # Items to export into callers namespace by default. Note: do not export |
|---|
| 14 | # names by default without a very good reason. Use EXPORT_OK instead. |
|---|
| 15 | # Do not simply export all your public functions/methods/constants. |
|---|
| 16 | @EXPORT = qw( |
|---|
| 17 | set_inGlobalEvalState |
|---|
| 18 | ); |
|---|
| 19 | $VERSION = '0.01'; |
|---|
| 20 | |
|---|
| 21 | # uncomment next line to debug dyna-loading ARB.so: |
|---|
| 22 | # $DynaLoader::dl_debug = 1; |
|---|
| 23 | |
|---|
| 24 | bootstrap ARB $VERSION; |
|---|
| 25 | # bootstrapping succeeded (would die otherwise) |
|---|
| 26 | $in_global_eval = 0; # now allow to call prepare_to_die() |
|---|
| 27 | |
|---|
| 28 | # globally catch die, redirect to arb_message, then confess |
|---|
| 29 | package CORE::GLOBAL; |
|---|
| 30 | use subs 'die'; |
|---|
| 31 | |
|---|
| 32 | sub ARB::set_inGlobalEvalState($) { |
|---|
| 33 | # hack to make eval-based perl-scripts work with die-catcher (below in 'sub die'). |
|---|
| 34 | # |
|---|
| 35 | # if your script uses eval, protect the top-level eval as follows: |
|---|
| 36 | # |
|---|
| 37 | # eval { |
|---|
| 38 | # set_inGlobalEvalState(1); |
|---|
| 39 | # do_something_that_may_die(); |
|---|
| 40 | # }; |
|---|
| 41 | # set_inGlobalEvalState(0); |
|---|
| 42 | # if ($@) { die $@; } # this call of die will trigger prepare_to_die() below. |
|---|
| 43 | # |
|---|
| 44 | my ($state) = @_; |
|---|
| 45 | $in_global_eval = $state; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | sub ARB::notify_and_wait($) { |
|---|
| 49 | # helper for macros (see ticket #856 for whole story): |
|---|
| 50 | # |
|---|
| 51 | # - opens popup displaying 'notification' and a continue button. |
|---|
| 52 | # |
|---|
| 53 | # - until pressing the button: |
|---|
| 54 | # - the macro pauses execution. |
|---|
| 55 | # - arb GUI may be accessed by user to change settings and/or |
|---|
| 56 | # select species etc. |
|---|
| 57 | # |
|---|
| 58 | # - after pressing the continue button: |
|---|
| 59 | # - macro execution continues. |
|---|
| 60 | # |
|---|
| 61 | my ($notification) = @_; |
|---|
| 62 | system("arb_wetc -notify \"".$notification."\" -button \"Click when done to continue with macro\""); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | sub show_arb_message($) { |
|---|
| 66 | my ($msg) = @_; |
|---|
| 67 | $msg =~ s/\n/\\n/g; |
|---|
| 68 | $msg =~ s/'/"/g; |
|---|
| 69 | system("arb_message '$msg'"); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | sub die { |
|---|
| 73 | if ($already_dying==0 and $in_global_eval==0) { |
|---|
| 74 | $already_dying++; # do not recurse |
|---|
| 75 | |
|---|
| 76 | ARB::prepare_to_die(); # abort all transactions and close all databases (too avoid deadlock; see #603) |
|---|
| 77 | |
|---|
| 78 | my ($msg) = @_; |
|---|
| 79 | $msg =~ s/\n+$//g; # remove trailing LFs |
|---|
| 80 | my $errname = 'arb macro/perl execution error'; |
|---|
| 81 | if ($msg eq '') { $msg = 'unknown '.$errname; } |
|---|
| 82 | else { $msg = "$errname: '$msg'"; } |
|---|
| 83 | show_arb_message($msg."\n(see console for details)"); |
|---|
| 84 | |
|---|
| 85 | use Carp; |
|---|
| 86 | Carp::confess("$msg"); # recurses into this sub |
|---|
| 87 | } |
|---|
| 88 | else { |
|---|
| 89 | CORE::die @_; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | # Preloaded methods go here. |
|---|
| 94 | |
|---|
| 95 | # Autoload methods go after =cut, and are processed by the autosplit program. |
|---|
| 96 | |
|---|
| 97 | 1; |
|---|
| 98 | __END__ |
|---|
| 99 | # Below is the stub of documentation for your module. You better edit it! |
|---|
| 100 | |
|---|
| 101 | =head1 NAME |
|---|
| 102 | |
|---|
| 103 | ARB - Perl extension for ARB |
|---|
| 104 | |
|---|
| 105 | =head1 SYNOPSIS |
|---|
| 106 | |
|---|
| 107 | use ARB; |
|---|
| 108 | |
|---|
| 109 | =head1 DESCRIPTION |
|---|
| 110 | |
|---|
| 111 | The ARB perl module provides access to a ARB databases. You may |
|---|
| 112 | connect to a remote database (e.g. a running instance of ARB_NTREE) or |
|---|
| 113 | open your own database. |
|---|
| 114 | |
|---|
| 115 | =head1 AUTHOR |
|---|
| 116 | |
|---|
| 117 | ARB development, devel@arb-home.de |
|---|
| 118 | |
|---|
| 119 | =head1 SEE ALSO |
|---|
| 120 | |
|---|
| 121 | perl(1). |
|---|
| 122 | |
|---|
| 123 | =cut |
|---|