santhanamv has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
Greetings.!!!!
I have a small doubt in variable substituation in eval function. Please see the below code and output.
===============
$aa = 4; $bb = 0; eval { $cc = $aa/$bb; }; if ( $@ ) { print "Wrong = Divide by Zero\n"; } else { print "No Exception = Continue your code here\n"; }
OUTPUT:
Wrong = Divide by Zero
===============
But if I use the constant instead of variable, the exception catch is not happening. I am getting the actual run time error.
===============
eval { $cc = 4/0; }; if ( $@ ) { print "Wrong = Divide by Zero\n"; } else { print "No Exception = Continue your code here\n"; }
OUTPUT:
Illegal division by zero at test.pl line 2.
===============
Could anyone let me know the reason for the different output for the same code.
Edited by planetscape - added code tags and rudimentary formatting
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Variables in eval funcation
by tinita (Parson) on Sep 06, 2006 at 12:41 UTC | |
|
Re: Variables in eval funcation
by davorg (Chancellor) on Sep 06, 2006 at 12:43 UTC | |
|
Re: Variables in eval funcation
by imp (Priest) on Sep 06, 2006 at 12:44 UTC | |
|
Re: Variables in eval funcation
by diotalevi (Canon) on Sep 06, 2006 at 13:18 UTC | |
|
Re: Variables in eval funcation
by NetWallah (Canon) on Sep 06, 2006 at 12:43 UTC |