perl exceptions and return value in finally block

R

rusland

Hello !

I need to know return value from try block in finally block.

For example, I have code like this
try {
code 1...
return x1 if ...
code 2...
return x2 if ...
etc...
}
finally {
my $ret = $how_to_get_return_value_from_try_block; #?
};

So finally block not only for exception processing, but also
for processing of return code from try block.

Is any build into perl mechanisms to let this value know
in finally block? I study man page of Error.pm and not found
any such possibility.
 
J

J. Gleixner

Hello !

I need to know return value from try block in finally block.

For example, I have code like this
try {
code 1...
return x1 if ...
code 2...
return x2 if ...
etc...
}
finally {
my $ret = $how_to_get_return_value_from_try_block; #?
};

So finally block not only for exception processing, but also
for processing of return code from try block.

Is any build into perl mechanisms to let this value know
in finally block? I study man page of Error.pm and not found
any such possibility.

You can't "return" from a try.

my $ret;
try {
if( condition ) { $ret = 'abc' }
elsif( condition2 ) { $ret='lmnop'; }
}
 
R

rusland

You can't "return" from a try.

my $ret;
try {
if( condition ) { $ret = 'abc' }
elsif( condition2 ) { $ret='lmnop'; }

Yes, I can use return at try, for example:
#!/usr/bin/perl

use strict;
use Error qw:)try);

sub apple {
try {
return 'fruit' if $_[0] eq 'apple';
return 'possibly a fruit';
}
finally {
print 'finally at apple(): ';
};
}

print apple('apple'), "\n";
print apple('berry'), "\n";

exit;


But problem is that I do not know what is return value in the finally
block... :(
 
B

Ben Morrow

Quoth (e-mail address removed):
You can't "return" from a try.

my $ret;
try {
if( condition ) { $ret = 'abc' }
elsif( condition2 ) { $ret='lmnop'; }

Yes, I can use return at try, for example:
#!/usr/bin/perl

use strict;
use Error qw:)try);

sub apple {
try {
return 'fruit' if $_[0] eq 'apple';
return 'possibly a fruit';
}
finally {
print 'finally at apple(): ';
};
}

print apple('apple'), "\n";
print apple('berry'), "\n";

exit;


But problem is that I do not know what is return value in the finally
block... :(

Don't do that then.

sub apple {
my $rv;

try {{
$rv = 'fruit', last if $_[0] eq 'apple';
$rv = 'possibly a fruit';
}}
finally {
print 'finally at apple(): $rv';
}
}

Note the {{ }} on the try: one set delimits the try block, the second
set creates a bare block so that last works.

Ben
 
J

J. Gleixner

Ben said:
Quoth (e-mail address removed):

OK, in your original example you had the try in an eval, I think. You
can 'return' from a subroutine.
#!/usr/bin/perl

use strict;
use Error qw:)try);

sub apple {
try {
return 'fruit' if $_[0] eq 'apple';
return 'possibly a fruit';
}
finally {
print 'finally at apple(): ';
};
}

print apple('apple'), "\n";
print apple('berry'), "\n";

exit;


But problem is that I do not know what is return value in the finally
block... :(

Don't do that then.

sub apple {
my $rv;

try {{

OP - take a look at $_[0] here.. it's not what you think it is.
$rv = 'fruit', last if $_[0] eq 'apple';
$rv = 'possibly a fruit';
}}
finally {
print 'finally at apple(): $rv';
print "finally at apple(): $rv";

Or depending on the use, simply return $rv;

return $rv;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,051
Members
47,656
Latest member
rickwatson

Latest Threads

Top