Subject: Re: Exception Handling in perl From: David Shere <dsh...@steelerubber.com> To: "Sarsamkar, Paryushan" <psars...@ptc.com> Copies to: beginners@perl.org Date sent: Fri, 27 Feb 2009 09:00:41 -0500
> On Fri, 2009-02-27 at 08:49 -0500, David Shere wrote: > > You can also have it do more than one thing: > > copy ("C:\\build.xml","D:\\build.xml") or ( print "Cannot copy : $!" and > > somethingElse() ); > > Or you can have more fun: > unless (copy ("C:\\build.xml","D:\\build.xml")) { > print "Cannot copy : $!"; > somethingElse(); > oneMoreThing(); > yetAnotherThing(); > } > > although I'm not sure if the "$!" is in scope in this example. $! doesn't care about scopes. It contains the error result of the last system call. No matter the blocks, loops, subroutine calls/returns ... Another option is copy ("C:\\build.xml","D:\\build.xml") or do { print "Cannot copy : $!"; somethingElse(); oneMoreThing(); yetAnotherThing(); }; In case there are several things that can fail and you want to handle them all on the same place the same way you can use something like eval { copy ("C:\\build.xml","D:\\build.xml") or die "Cannot copy build.xml: $!" copy ("C:\\report.xml","D:\\report.xml") or die "Cannot copy report.xml: $!" copy ("C:\\list.xml","D:\\list.xml") or die "Cannot copy list.xml: $!" } or reportTheErrorToTheUser( $@); If you need more advanced exception handling have a look at Exception::Class (and Exception::Class::Nested). Jenda ===== je...@krynicky.cz === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/