On 9/19/11 Mon  Sep 19, 2011  5:11 PM, "Parag Kalra" <paragka...@gmail.com>
scribbled:

> Hi,
> 
> I was getting this error message for one of my script.
> 
> The reason came out out to be, I had not place a semi-colon at the end of
> try-catch block.
> 
> try {
>    something
> } catch some_exception {
>   do something
> }
> 
> After I placed the semi-colon, I am no longer getting this error (Can't use
> string ("1") as a HASH ref while "strict refs")
> 
> try {
>    something
> } catch some_exception {
>   do something
> };
> 
> My questions is I have quite a few scripts that are using the SAME try-catch
> block without a semi-colon but those are working seamlessly. They why was I
> getting the error for only this try-catch block.
> 
> Is there some rule which we need to follow while using try-catch in Perl?

Yes: always end your try-catch statement with a semicolon! :)

The try-catch mechanism is not part of the Perl language (not as of Perl
5.10, anyway). There are modules that provide try-catch via prototyped
functions try(&$) and catch(&) that take code blocks as their first
argument. These include Error.pm, TryCatch.pm, Try::Tiny.pm, Exception.pm,
and Fatal.pm (according to
<http://en.wikipedia.org/wiki/Exception_handling_syntax#Perl>).

Since these are functions and not language keywords, they require a
semicolon at the end so that the subsequent statements are not interpreted
as additional catch() {} blocks.

Are you using one of these modules?

The result of leaving off the semicolon probably depends upon what follows
after the last catch block. While most of your programs do not issue that
same error message, it is still likely that they are not doing what you want
them to do.

If you want further help, please post a complete, short program that
demonstrates your problem.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to