On 20/09/2011 01:11, Parag Kalra wrote:
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?
Hi Parag The first problem I see is that you are passing 'catch' the return value of the subroutine 'some_exception' when it expects a code block. The correct syntax is try { <some code that may cause an exception> } catch { <handle exception passed in $_> } The way try / catch works is obscure, and presented in Programming Perl as an example of how subroutine prototypes could be useful, but the declaration of try looks like sub try (&$); so to express its functionality your code should be formatted like this try ( { <some code that may cause an exception> }, catch ({ <handle exception passed in $_> }) ); which clearly requires a semicolon after the closing parenthesis. For me, the bottom line is that try / catch is a funky showpiece that pushes Perl syntax beyond its limits. No one who sees your code will thank you for using it, and you should remove it in preference of a simple check on $@. It cannot be a good sign if you don't understand where the semicolons should go! HTH, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/