in reply to return from subfunction

In such cases, I usually prefer to use exceptions, i.e. die / eval.

sub foo { open F, ... or die "-1\nerror opening file"; return $result; } my $string; eval { $string = foo(); }; if ( $@ ) { ( my $code, $string ) = split /\n/, $@; ... }

If you really don't want to go that route, then you could return a return "object" which contains multiple fields. (It doesn't have to be a blessed object, though.)

sub foo { open F, ... or return( [ -1, "error opening file" ] ); return( [ 0, $result ] ); } my( $code, $string ) = foo();

Lastly: realize that die can return an object, not just a string. This can be pretty useful in cases such as this.

sub foo { open F, ... or die [ -1, "error opening file" ]; return $result; } my $string; eval { $string = foo(); }; if ( $@ ) { ( my $code, $string ) = $@; ... }
I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Replies are listed 'Best First'.
Re^2: return from subfunction
by fidesachates (Monk) on Apr 07, 2011 at 19:32 UTC
    Perfect! Thank you.