For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > October 2004 > Problem with 'eval' used in callbacks









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with 'eval' used in callbacks
Paul Bodenstab

2004-10-19, 3:57 pm

Problem:

I am accustomed to using the eval function to perform exception
detection and handling so my perl application does not die. However,
when I try to do this in a Perl/Tk application where the 'eval' occurs
in a callback procedure I am finding out that the application dies
instead of setting the $@ variable and continuing. This is certainly
annoying to this Perl/Tk newbie (although I have had many years of
non-Tk perl experience).

Hopefully this problem has been seen before and there is a workaround
for this problem. If so, could one of you kind folk please pass it on to me?

Thanks a bunch,

Paul



----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Ala Qumsieh

2004-10-19, 3:57 pm

Paul Bodenstab wrote:
> Problem:
>
> I am accustomed to using the eval function to perform exception
> detection and handling so my perl application does not die. However,
> when I try to do this in a Perl/Tk application where the 'eval' occurs
> in a callback procedure I am finding out that the application dies
> instead of setting the $@ variable and continuing. This is certainly
> annoying to this Perl/Tk newbie (although I have had many years of
> non-Tk perl experience).
>
> Hopefully this problem has been seen before and there is a workaround
> for this problem. If so, could one of you kind folk please pass it on to
> me?


I haven't seen this behaviour before, and have used eval() successfully
in Callbacks before with no problems. Can you post an example that
exhibits this behaviour?

--Ala

Paul Bodenstab

2004-10-19, 8:56 pm

Here is the actual code. The code implements a simple Perl/Tk shell.
When the code is executed the prompt 'PERL/TK SHELL 0> ' appears and one
can type any valid perl expression and it will execute once <Return> is
pressed. The result of the expression is printed and the user is once
again prompted for more input. This version allows only one line of
input at a time. The user input is contained in $_ and this is evaluated
within the eval function so any exceptions should not cause the entire
program to die but rather set the $@ variable appropriately and
continue. For example, when one types in '2*3 <Return>' the answer is
printed on the next line which is: '6'. The problem occurs if one types
'1/0' which should trigger the divide by zero exception which in turn
sets the $@ variable and the error message should be printed out AND the
program should continue executing however! The following is the exact
result of this test on my system:

PERL/TK SHELL 0> 2*3
6
PERL/TK SHELL 1> 1/0
Illegal division by zero at (eval 7) line 2, <_GEN_0> line 2.
PERL/TK SHELL 2> (program died at this point and displays the bash
prompt here)

It is clear that the exception did occur and that the program made it to
the second prompt but it is not clear why it died at that point.
Shouldn't the use of eval be proper here?

Any insight on this behavior will be greatly appreciated.

Thanks,

Paul


-------------------cut here-----------------
#!/usr/bin/perl -w

use Tk;

sub ptkcmd {
$cnt++;
$_=<STDIN>;
chop;
print eval( $_ ), $@;
print "PERL/TK SHELL $cnt> ";
}

$,="\n";
$cnt=0;

$mw = MainWindow->new;
$mw -> fileevent( 'STDIN', 'readable' => \&ptkcmd );
print "PERL/TK SHELL $cnt> ";
MainLoop;
-------------------cut here-------------------


Ala Qumsieh wrote:
> Paul Bodenstab wrote:
>
>
>
> I haven't seen this behaviour before, and have used eval() successfully
> in Callbacks before with no problems. Can you post an example that
> exhibits this behaviour?
>
> --Ala
>




----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Slaven Rezic

2004-10-19, 8:56 pm

Paul Bodenstab <paulbodenstab@thriventweb.net> writes:

> Problem:
>
> I am accustomed to using the eval function to perform exception
> detection and handling so my perl application does not die. However,
> when I try to do this in a Perl/Tk application where the 'eval' occurs
> in a callback procedure I am finding out that the application dies
> instead of setting the $@ variable and continuing. This is certainly
> annoying to this Perl/Tk newbie (although I have had many years of
> non-Tk perl experience).
>
> Hopefully this problem has been seen before and there is a workaround
> for this problem. If so, could one of you kind folk please pass it on
> to me?
>


I don't see a problem with dying Perl/Tk applications. On the
contrary: once your application is in the MainLoop, an exception is
not fatal anymore. You get an error dialog, but it's possible to
continue the application.

Can you be more specific how you use callbacks and eval together, i.e.
with a small code example?

Regards,
Slaven

--
Slaven Rezic - slaven <at> rezic <dot> de

tktimex - project time manager
http://sourceforge.net/projects/ptktools/
Slaven Rezic

2004-10-19, 8:56 pm

Paul Bodenstab <paulbodenstab@thriventweb.net> writes:

> Here is the actual code. The code implements a simple Perl/Tk shell.
> When the code is executed the prompt 'PERL/TK SHELL 0> ' appears and
> one can type any valid perl expression and it will execute once
> <Return> is pressed. The result of the expression is printed and the
> user is once again prompted for more input. This version allows only
> one line of input at a time. The user input is contained in $_ and
> this is evaluated within the eval function so any exceptions should
> not cause the entire program to die but rather set the $@ variable
> appropriately and continue. For example, when one types in '2*3
> <Return>' the answer is printed on the next line which is: '6'. The
> problem occurs if one types '1/0' which should trigger the divide by
> zero exception which in turn sets the $@ variable and the error
> message should be printed out AND the program should continue
> executing however! The following is the exact result of this test on
> my system:
>
> PERL/TK SHELL 0> 2*3
> 6
> PERL/TK SHELL 1> 1/0
> Illegal division by zero at (eval 7) line 2, <_GEN_0> line 2.
> PERL/TK SHELL 2> (program died at this point and displays the bash
> prompt here)
>
> It is clear that the exception did occur and that the program made it
> to the second prompt but it is not clear why it died at that point.
> Shouldn't the use of eval be proper here?
>


It seems to be a strange bug in conjunction with fileevent() --- other
Tk callbacks does not have this problem. Said this, an easy workaround
looks like this:

$mw -> fileevent( 'STDIN', 'readable' => sub { $mw->after(1, sub { ptkcmd() }) } );

Regards,
Slaven

--
Slaven Rezic - slaven <at> rezic <dot> de

Lost in your Tk widget tree? Try
http://user.cs.tu-berlin.de/~eserte.../Tk-WidgetDump/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com