Can't use string ("1") as a HASH ref while "strict refs"

--00151747694010009a04ad544f1b
Content-Type: text/plain; charset=UTF-8

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?

Thanks,
Parag

--00151747694010009a04ad544f1b--
0
paragkalra
9/20/2011 12:11:42 AM
perl.beginners 29388 articles. 4 followers. Follow

8 Replies
1463 Views

Similar Articles

[PageSpeed] 57

On 9/19/11 Mon  Sep 19, 2011  5:11 PM, "Parag Kalra" <paragkalra@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.


0
jimsgibson
9/20/2011 1:08:14 AM
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
0
rob
9/20/2011 1:35:43 AM
>>>>> "Rob" == Rob Dixon <rob.dixon@gmx.com> writes:

Rob> For me, the bottom line is that try / catch is a funky showpiece that
Rob> pushes Perl syntax beyond its limits. No one who sees your code will
Rob> thank you for using it, and you should remove it in preference of a
Rob> simple check on $@.

Completely disagree.  Checking $@ *appears* to be easy, but is prone to
error.  Try::Tiny is best of breed to fix this. I would prefer people
use Try::Tiny instead of trying to handcraft the $@ checking and getting
it wrong.

For example, if you don't know what's wrong with this:

    eval { ... };
    if ($@) { ... }

then you *need* to start using Try::Tiny.

See especially the section beginning "BACKGROUND" on the Try::Tiny
manpage.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
0
merlyn
9/20/2011 2:57:03 PM
On 20/09/2011 15:57, Randal L. Schwartz wrote:
>> "Rob" == Rob Dixon<rob.dixon@gmx.com>  writes:
>
> Rob>  For me, the bottom line is that try / catch is a funky showpiece that
> Rob>  pushes Perl syntax beyond its limits. No one who sees your code will
> Rob>  thank you for using it, and you should remove it in preference of a
> Rob>  simple check on $@.
>
> Completely disagree.  Checking $@ *appears* to be easy, but is prone to
> error.  Try::Tiny is best of breed to fix this. I would prefer people
> use Try::Tiny instead of trying to handcraft the $@ checking and getting
> it wrong.
>
> For example, if you don't know what's wrong with this:
>
>      eval { ... };
>      if ($@) { ... }
>
> then you *need* to start using Try::Tiny.
>
> See especially the section beginning "BACKGROUND" on the Try::Tiny
> manpage.

I don't have Try::Tiny installed, but will take a look. If you mean that
$@ may be accessed by several pieces of code when a program dies, then I
agree, but my main concern is that try / catch are both simply
prototyped subroutines, and the compile can not tie things down to requiring

   try BLOCK catch BLOCK;

Maybe Try::Tiny works differently but I can't see how it could cover this.

Rob
0
rob
9/21/2011 7:10:00 PM
>>>>> "Rob" == Rob Dixon <rob.dixon@gmx.com> writes:

Rob> I don't have Try::Tiny installed, but will take a look.

I have an addressbar query shortcut of:

  http://search.cpan.org/perldoc/#query#

aliased to "perldoc", so I can type "perldoc Try::Tiny" and get the
latest manpage on it directly from the CPAN without installing it.

Rob>  If you mean that
Rob> $@ may be accessed by several pieces of code when a program dies,

No.  See the referenced docs.

Rob>  then I agree, but my main concern is that try / catch are both
Rob> simply prototyped subroutines, and the compile can not tie things
Rob> down to requiring

Rob>   try BLOCK catch BLOCK;

Why does that matter?  It's the semantics that you have to get right,
not just the syntax.

Rob> Maybe Try::Tiny works differently but I can't see how it could
Rob> cover this.

Again, read the referenced doc.  It would have taken you about 5
minutes, and you wouldn't have had to write this entire ignorant reply
or waste my time replying to it.

:(

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
0
merlyn
9/21/2011 7:57:46 PM
On 21/09/2011 20:57, Randal L. Schwartz wrote:
>>>>>> "Rob" == Rob Dixon<rob.dixon@gmx.com>  writes:
> 
> Rob>  I don't have Try::Tiny installed, but will take a look.

I am back at my desk, and have this horror from you to respond to. I had
started to enjoy some of your posts, and hoped that your legendary
brutal negativity was beginning to lapse.

Whatever your thoughts, please drop the shield that says you are
responding to people's coding rather than the people themselves. The
difference is an age-old philosophy, and I would love to hear your
thoughts on the distinction.

> I have an addressbar query shortcut of:
> 
>    http://search.cpan.org/perldoc/#query#
> 
> aliased to "perldoc", so I can type "perldoc Try::Tiny" and get the
> latest manpage on it directly from the CPAN without installing it.

Hooray for you. I use my editor as an IDE to do the very same thing.

> Rob>   If you mean that $@ may be accessed by several pieces of code
> Rob>   when a program dies,
> 
> No.  See the referenced docs.

I have thought a lot about how an 'eval' block may be unsafe, and have
commented several times to discourage it.

I have now read the POD for Try::Tiny, and I cannot see anything that I
hadn't already envisaged: that when a Perl process dies $@ may be
accessed by several pieces of code.

The Try::Tiny documentation says this

>     When you run an eval block and it succeeds, $@ will be cleared,
>     potentially clobbering an error that is currently being caught.

Oddly, you chose to misquote me by splitting a sentence. Your version of
my post is irregular and inappropriate. It is not what I would have written.

What I wrote was

> I don't have Try::Tiny installed, but will take a look. If you mean that
> $@ may be accessed by several pieces of code when a program dies, then I
> agree, but my main concern is that try / catch are both simply
> prototyped subroutines, and the compile can not tie things down to requiring
> 
>   try BLOCK catch BLOCK;

Your version:

 Rob>   then I agree, but my main concern is that try / catch are both
 Rob>  simply prototyped subroutines, and the compile can not tie things
 Rob>  down to requiring
 Rob> 
 Rob>    try BLOCK catch BLOCK;
 
> Why does that matter?  It's the semantics that you have to get right,
> not just the syntax.

I am sure you know the answer to that. It matters because the
presentation of the mechanism is deceptive. It looks very like

  while () {
    :
  }
  continue
    :
  }

but instead is something very different. As I have described, it amounts
to two obscure prototyped subroutine definitions. From the source:

  sub try (&;@);

and

  sub catch (&;@);

which is far from what the average man expects of 'while', 'for',
'continue' and so on. It is a deception, and should not be published
until the equivalent functionality is part of the Perl language.

> Rob>  Maybe Try::Tiny works differently but I can't see how it could
> Rob>  cover this.
> 
> Again, read the referenced doc.  It would have taken you about 5
> minutes, and you wouldn't have had to write this entire ignorant reply
> or waste my time replying to it.

Randal, as I opened, I had started to enjoy and respect your posts here
and elsewhere. You were never required to reply to my 'ignorant post',
yet you chose to show your foolish emptiness once again.

It seems to me that you place your entire worth in your extreme and
detailed knowledge of Perl and its teaching. Please believe me that, as
a fellow human being, I would love to hear about your personal
experiences and how programming has played a part in your character.

You certainly seem insecure, and your silliness about your criticism not
applying to a person but to their deeds needs to be washed ashore. Stop
it and be real. Believe me, you wouldn't cease to exist if you lost your
technical ability.

Rob
0
rob
9/24/2011 1:38:38 AM
>>>>> "Rob" == Rob Dixon <rob.dixon@gmx.com> writes:

Rob> Randal, as I opened, I had started to enjoy and respect your posts here
Rob> and elsewhere. You were never required to reply to my 'ignorant post',
Rob> yet you chose to show your foolish emptiness once again.

But I am highly motivated to reduce and contain the damage caused by
misinformation.  You were spreading misinformation to others.  If my
reply to you had no impact on you, that is sad, but at least the others
reading this thread will be more enlightened.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
0
merlyn
9/27/2011 4:35:05 PM
On 27/09/2011 17:35, Randal L. Schwartz wrote:
>>>>>> "Rob" == Rob Dixon<rob.dixon@gmx.com>  writes:
>
> Rob>  Randal, as I opened, I had started to enjoy and respect your posts here
> Rob>  and elsewhere. You were never required to reply to my 'ignorant post',
> Rob>  yet you chose to show your foolish emptiness once again.
>
> But I am highly motivated to reduce and contain the damage caused by
> misinformation.  You were spreading misinformation to others.  If my
> reply to you had no impact on you, that is sad, but at least the others
> reading this thread will be more enlightened.

If you think you are doing the community a favour by making it a
threatening place place where people can expect their ideas to be met
with sardonic derision then all I can do is ask you to think again. I
doubt if I can say anything more to change your mind.

Rob
0
rob
9/28/2011 2:08:28 AM
Reply:

Similar Artilces:

Can't use string ("1") as a HASH ref while "strict refs" in use at ./policy.sanitizer line 255.
Hi there, I am trying to figure out why the following lines are giving me an error: --- snip --- $found{filter}{$filter}{family}{$family} = 1; print keys %{ $found{filter}{$filter}{family} }; --- snip ---- here's the error Can't use string ("1") as a HASH ref while "strict refs" in use at ../policy.sanitizer line 255. I am trying to the display all the keys for $found{filter}{$filter}{family} please Cheers, Noah On 10/9/09 Fri Oct 9, 2009 2:12 PM, "Noah" <noah-list@enabled.com> sc...

Can't use string ("3862926") as a HASH ref while "strict refs"
I guess I'm not understanding what I'm doing (or have been reading in my web searches)... Can someone explain the difference between these two code snippets... SUN83-PRODWEB>more foo #! /usr/local/bin/perl use strict; my %t; my ($a, $c); my ($aa, $cc); $a=3D$aa=3D1; $c=3D$cc=3Dq{Additional Electrical Circuit}; $t{$a}{$c}{AMT}=3D1000; $c=3Dq{Additional}; $t{$a}{$c}{AMT}=3D2; print $t{$aa}{$cc}{AMT}, "\n"; &Ix($t{$aa}{$cc}{AMT}); sub Ix { my ($v) =3D @_; ...

bugzilla: undef error
I'm encountering the problem described here: https://bugzilla.mozilla.org/show_bug.cgi?id=300448 I'm running bugzilla 2.18.3 on fedora. The problem seems to be that a line was added to this file: template/en/default/bug/create/created.html.tmpl So I commented out this line: mailrecipients = item.mailrecipients and restarted everything, but I'm still getting the same error. I'm not sure that I understand the solution very well. Any ideas? Thanks, Bryan ...

DBD::Proxy::db table_info failed: Server returned error: Failed to execute method CallMethod: Can't use string ("1") as an ARRAY ref while "strict refs" in use
------_=_NextPart_001_01C2BB46.4379CEA8 Content-Type: multipart/related; type="multipart/alternative"; boundary="----_=_NextPart_002_01C2BB46.4379CEA8" ------_=_NextPart_002_01C2BB46.4379CEA8 Content-Type: multipart/alternative; boundary="----_=_NextPart_003_01C2BB46.4379CEA8" ------_=_NextPart_003_01C2BB46.4379CEA8 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I'm having a problem using the table_info() via ProxyServer... I'm = getting the following error messages: I am us...

Can't use string as a subroutine ref while "strict refs"
Hello, I am trying to do the following, do I need to remove the strict option for this to work or is there a better way ? --snip-- sub admin { my $term = shift; my $host = shift; my @actions = ( "ssh_login", "su_login", "su_logout", "ssh_logout", "cmds" ); foreach my $a (@actions) { &show_log("Calling $a"); my $check= &{$a}; return(1) unless $check==0; } } --snip-- -- Michael Gale Red Hat Certified Engineer Network Administrator Pason Systems Corp. You could...

Can't use string as a subroutine ref while "strict refs"
------=_NextPart_000_0004_01C70428.644E8960 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable use strict; use warnings; use CGI; my $q =3D new CGI; my $do =3D $q->param('do') || 'main''; if ($do) { &$do; } sub main { blah blah } =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Trying to call the subroutine main from variable $do but I am gettin' = error: Can't use string ("main") as a subroutin...

lwp-rget error message ("can't use string ("URI::URL") as ARRAY ref)
I've been using lwp-rget, for instance: cyc$ lwp-rget --depth=2 --limit=100 "http://www.somesite.com/~whoever with no problems. tonight I got this msg: Can't use string ("URI::URL") as an ARRAY ref while "strict refs" in use at /usr/local/path_to_module/URI/WithBase.pm line 41. version info: This is perl, version 5.005_03 built for i386-freebsd and: This is lwp-rget version 1.19 (libwww-perl-5.52) Any advice would be really appreciated. CYC. Hi! I have a simple program which opens each URL in a file and saves ...

Can't use string ("") as an Array ref
I'm getting the following when trying running the following code with use strict; without it, it works fine.=20 =23=21/usr/bin/perl -w=20 use strict;=20 use warnings;=20 use Data::Dumper;=20 my (=40data,=40contents);=20 =40data =3D <STDIN>; push(=40contents,map =7B =5B split/:/,=24_ =5D if/=5E=5Cw+/ =7D (sort = =40data) );=20 for(1..=24=23contents) =7B=20 print =24contents=5B=24_->=5B0=5D . =22=5Cn=22 if = defined(=24contents=5B=24_=5D->=5B0=5D);=20 =7D - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - = - - - - - - - Th...

Can't use string ("bug/create/create-guided.html.tm") as a HASH ref
- I could use some help with my Bugzilla install - I have installed the stable release of Bugzilla (2.16.5) and have customized some of the templates (which is a cool new way to do things). I am following the instructions to modify create.html.tmpl and comment.txt.tmpl to create some fields for bug entry. However, I get the following perl error message and I can't figure out why: Can't use string ("bug/create/create-guided.html.tm") as a HASH ref while "strict refs" in use at /usr/lib/perl5/site_perl/5.8.0/Template/Provider.pm line 663. I am new to...

Using "+" or "||"
Using SQLAnywhere 5.5.04, I've gotten into the habit of using "||" in ISQL to indicate a string concatenation. I needed to paste my SQL statement into the PowerBuilder script painter for some embedded SQL, and PB didn't like the "||" very much at all. I changed it to "+" and it seems to be ok. Do these two operators indicate ~exactly~ the same thing? moin, afaik these two's are not the same! if you're using "||" and any term is NULL then in the resultstring the term will be ignored if you use "+" then the resu...

"Using" or "With"
Hi all Please can someone enlighten to me as regards the difference with the "Using" and "With" statement when accessing data - which is better, what are the limitations and/or any pointers. Many thanks. Regards DaveDavid WinchesterPlease mark as answer if this is the solution.  using gives you the ability to use the connection and it closes the connection directlly after you finish using it. and there is no need to try- cach - finaly. there is no limitation on using USING keywordMuhanad YOUNISMCSD.NETMy Blog || My Photos || LinkedIn I have a dataobject the re...

(setxkbmap) can't use "ALT_GR"/"left ALT" button after using it
hello folks i use LXDE as i'm known and my only solution to switch keyboard is to use setxkbmap now after i define keyboards layouts to switch and chose ALT+Shift to switch it goes "bugged" using the right ALT will switch the layouts and i'm not able to write the "other" letters help please P.S : this is the command i use �setxkbmap -option grp:switch,grp:alt_shift_toggle,grp_led:scroll fr,ar� -- SAFAD ------------------------------------------------------------------------ bump up rotfl! -- SAFAD -------------------------------...

Precedence of "where" ("of", "is", "will")?
Nobody on #perl6 today could answer this one. Is: Str | Int where { $_ } the same as: (Str | Int) where { $_ } or: Str | (Int where { $_ }) ? Followup questions, Mr. President: What kind of operators are "where", "of", "is", and "will"? Is there a reason that S03 doesn't list them? What are their precedence(s)? -- Chip Salzenberg - a.k.a. - <chip@pobox.com> Open Source is not an excuse to write fun code then leave the actual work to others. Chip Salzenberg writes: &...

.ALLCOL("%COLUMN%", " ", ", ", ", ")
Do you know anyway for me to exclude a subset of columns returned by this function. We have two columns (rec_user and rec_datetime) which are in all of our tables, but when generating triggers I want automatically generate a script which does not include those two columns but does include all other columns in that table. Bruce I should add that I am using PD 9.0.0.580. Bruce "Bruce Lamb" <lamb.bruce@mayo.edu> wrote in message news:6HgI315nCHA.155@forums.sybase.com... > Do you know anyway for me to exclude a subset of columns returned by this > function. ...

Web resources about - Can't use string ("1") as a HASH ref while "strict refs" - perl.beginners

Resources last updated: 12/14/2015 9:59:18 AM