Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all
I would like to use exception handling (try-catch-throw method) without using any CPAN module.
My code is as follows:
$request = "abc"; $response = "test"; sub request_response { my($request,$response) = @_; my $success; if(($request =~ /^JI/) || ($request eq "JO")) { $success = 1; $request = "1A Transmission:\n".$request; } elsif(($response eq "OK INFORMATION DELETED") || ($response =~ /DESCRI +PTION ELEMENT DOES NOT EXIST/)) { $success = 1; $response = "1A Response:\n".$response; } my $transaction = join("\n",$request,$response); print STDOUT $transaction."\n"; print STDERR $transaction unless ($success == 1); }
Can anyone please tell me to implement the try-catch-throw method in this code.
Thanks
  • Comment on pls help me to implement try-catch-throws without using any exception handling
  • Download Code

Replies are listed 'Best First'.
Re: pls help me to implement try-catch-throws without using any exception handling
by DamnDirtyApe (Curate) on Aug 30, 2004 at 19:07 UTC

    Put the "try" code inside an eval block, then check the value of $@. Example:

    #! /usr/bin/perl -w use strict; for my $i ( 2, 1, 0 ) { for my $j ( 2, 1 ) { eval { print "$j / $i = " . $j / $i . $/; }; if ( $@ ) { warn "Warning: $@"; } } } __END__

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: pls help me to implement try-catch-throws without using any exception handling
by duff (Parson) on Aug 30, 2004 at 18:59 UTC
Re: pls help me to implement try-catch-throws without using any exception handling
by csuhockey3 (Curate) on Aug 30, 2004 at 19:35 UTC
    Check out this thread on the same topic.
      I think Error (perldoc Error) comes with perl (does not require going to CPAN and installing) if that's what you're worried about. Eval may be easier for simple cases though.
        Error is not a core module. Errno is, but not Error. See Module::CoreList.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.