Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

WWWXXX DACB, wwwxxx,nvif, why atm producer might be held liable for economic injury, wwwxxxy=ServiceLogin, w2ksp4.exe, WwwxxXdbf, procmail "FROM_MAILER" patch, Use of assignment to $[ is deprecated at /usr/local/sbin/apxs line 86. , wwwxxx vim, mysql closing table and opening table

Links

XODOX
Impressum

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

Posted on 2011-09-20 02:11:42 by Parag Kalra

--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--

Report this message

#2: Re: Can"t use string ("1") as a HASH ref while "strict refs"

Posted on 2011-09-20 03:08:14 by Jim Gibson

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.



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

Report this message

#3: Re: Can"t use string ("1") as a HASH ref while "strict refs"

Posted on 2011-09-20 03:35:43 by Rob Dixon

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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#4: Re: Can"t use string ("1") as a HASH ref while "strict refs"

Posted on 2011-09-20 16:57:03 by merlyn

>>>>> "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

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

Report this message

#5: Re: Can"t use string ("1") as a HASH ref while "strict refs"

Posted on 2011-09-21 21:10:00 by Rob Dixon

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

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

Report this message

#6: Re: Can"t use string ("1") as a HASH ref while "strict refs"

Posted on 2011-09-21 21:57:46 by merlyn

>>>>> "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

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

Report this message

#7: Re: Can"t use string ("1") as a HASH ref while "strict refs"

Posted on 2011-09-24 03:38:38 by Rob Dixon

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

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

Report this message

#8: Re: Can"t use string ("1") as a HASH ref while "strict refs"

Posted on 2011-09-27 18:35:05 by merlyn

>>>>> "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

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

Report this message

#9: Re: Can"t use string ("1") as a HASH ref while "strict refs"

Posted on 2011-09-28 04:08:28 by Rob Dixon

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

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

Report this message