On Fri, 15 Feb 2002, Paul Farley wrote:

> I have an application that I am using a perl script to do pattern matching
> and then execute commands specific to that application.  I need a way to
> handle an error in my script if one of the `cmd blah`; doesn't work and send
> it to an error subroutine.
>
> Here's what I have now:
> <SNIPPED>
>
> sub subscription
>  {
> ($passed_ep,$passed_pr$passed_sub,$passed_org,$passed_plat)=shift;
>
>   #add error routine to handle if PR/PM doesn't exist -pass to
> exception_sub?
>    `wsub \@ProfileManager:Staging \@Endpoint:$passed_ep`;
>    `wln \@Endpoint:$passed_ep \@PolicyRegion:$passed_pr\.$passed_org`;
>    `wsub \@ProfileManager:$passed_sub\.$passed_org\.$passed_plat\.ep
> \@Endpoint:$passed_ep`;
>    # on error pass ($ep,$pr,$sub,$org,$plat)
>  }

You should use the system command here.  You are using `` in a void
context, and it's unnecessary to do so, unless you are doing something
with the output strings.

I would do something like:

  sub mysub {
    ...
    system("cmd", @args) == 0
      or die "Error running com: $?\n";
    ...
  }

  sub handle_error {
    my $error_msg = shift;

    print "$error_msg\n";
    print LOG "$error_msg\n";
    ...
  }

And handle the errors this way:

eval {
  mysub();
};

handle_error($@) if $@;

See the perldoc on eval -- it provides a fairly easy way to throw and
catch exceptions -- die throws the exception, eval catches it, and puts
the error message from die into $@.

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
Alcohol, hashish, prussic acid, strychnine are weak dilutions. The surest
poison is time.
                -- Emerson, "Society and Solitude"


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to