Hi Nicholas,

The perlvar manpage indicates that you may only assign
an errno to $!, making no mention of assigning to it
custom error messages.

Many modules work around this by defining a global
$Module::error or some such variable that defines the
last error message generated (.e.g, the $DBI::errstr
global). It's less cute than

 foo() or die "Can't foo: $!";

, but it's much more flexible.

Others might advocate the eval BLOCK statement
combined with the $@ built-in (see the perlfunc
manpage). In that case, rather than setting $!, you'd
die() (which sets $@ to the die()'s argument). And to
prevent your script from exiting upon die()ing, you'd
wrap your function call in an eval BLOCK statement,
and test the value of $@ afterwards. For (a contrived
and untested) example:

 sub divide {
   my($dividend, $divisor) = @_;
   die "Illegal divide by zero" if $divisor == 0;
   return $dividend / $divisor;
 }

 eval {
   my $answer = divide( 4 / 0 );
 };

 if($@) {
   "Couldn't divide: $@";
 }

Or if you want to go further, check out
Class::Exception. I haven't (directly) used it myself,
so I make no promises of its utility.

As always, there are probably more ways of handling
exceptions than you can shake a stick at. Thankfully
with Perl 6 we'll be getting something a little more
standard (whatever that means).

Cheers,
David

--- "Nicholas G. Thornton"
<[EMAIL PROTECTED]> wrote:
> (this seemed not to go through last time I sent it,
> daemon returned a failure)
> 
> I'm working on expanding a module I wrote a while
> back and one of the things I'm
> adding is an input filter. I'd like to have bad
> input return an error (to $!
> perhaps/preferably) so you could write something
> like:
> 
> &function() or die "Error: $!";
> 
> .. but if the input is fine it returns either the
> input (as it's currently
> designed) or a true value (the easier way). Either
> way I was wondering how to
> set $! since = doesn't seem to work.
> 
> ~wren
> 
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more
http://tax.yahoo.com

Reply via email to