[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cgi-list
Subject:    Re: [CGI] Exception Handling in PERL
From:       Jeff Pinyan <jeffp () crusoe ! net>
Date:       2000-08-29 18:23:01
[Download RAW message or body]

About the Warnings.pm module:

  #!/usr/bin/perl -w

  use Warnings (
    uninit => {
      -priority => 1,
      -catch => 'uninitialized',
      -handler => sub {
        warn "undefined value used in $_[1] at line $_[2].\n"
      },
    },
    not_numeric => {
      -priority => 2,
      -catch => "isn't numeric",
      -handler => sub {
        warn "string where number expected in $_[1] at line $_[2].\n"
      },
    }
  );

  print "foo" + undef;
  # Perl's warnings are printed here:
  #   Use of uninitialized value at...
  #   Argument "foo" isn't numeric in add at...

  activate $not_numeric;
  # label 'not_numeric' is attached to variable $not_numeric

  print "foo" + undef;
  # Perl's warning:
  #   Use of uninitialized value at...
  # your warning:
  #   string where number expected in...

  activate $uninit;

  print "foo" + undef;
  # your warnings:
  #   undefined value used in...
  #   string where number expected in...

There are more features (if a warning can be caught by two handlers, the
one with the higher priority is executed first, and you can set an option
to ONLY do ONE handler per warning):

  $foo = new Warnings 'alpha', sub { print "ALPHA" }, 1;
  $bar = new Warnings 'beta', sub { print "BETA" }, 2;

  warn "alphabeta";
  # ALPHA

  $foo->priority(3);

  warn "alphabeta";
  # BETA

  $Warnings::ONE_WARNING = 0;

  warn "alphabeta";
  # BETA
  # ALPHA

And there are still more features.  You can silence a warning handler so
that it isn't in effect.  You can turn all user-defined warning handlers
off.

To see the module (and the shoddy documentation), go to:

  http://www.pobox.com/~japhy/modules/Warnings/

-- 
Jeff "japhy" Pinyan     japhy@pobox.com     http://www.pobox.com/~japhy/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/
CPAN - #1 Perl Resource  (my id:  PINYAN)        http://search.cpan.org/


-----------------------------------------------------------------
To unsubscribe, send mail to "majordomo@jann.com" with "unsubscribe cgi-list"
in the body.

CalendarPlus v3.0 with JavaPlus is available NOW!
http://www.calendarplus.com/

The CGI Tips & Tricks website (and archive of the list) is located
at http://perl.jann.com/
-----------------------------------------------------------------

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic