Hi,
In ksh, I can do something like this:
blat()
{
print "blat says foo"
return 0
}
junk=`blat`
rv=$?
In which case, "$junk" holds "blat says foo", while "$rv" holds 0, or
whatever blat() actually returns. This is really useful in capturing
an error message from the function, as well as the actual return code.
How would I do this in Perl, where I capture the print() statements of
the sub in a variable, but the return value separately? Something
along the lines of:
sub blat ()
{
print "blat says foo";
return 0;
}
my( $junk, $rv );
$junk = `blat`; # -- this doesn't actually work, since blat() isn't
an external executable
$rv = $?;
What I want is whatever blat() might have output via print()
statements into $junk rather than STDOUT, but the return value of the
function in a separate variable; $rv in this case.
I know I can use something like:
sub blat ()
{
print "This will be sent to STDOUT\n";
die( "This will get caught by the eval{ ... }" );
}
eval { blat() };
print $@ if $@;
But this is different.
A. i'm limited to whatever I provide in die(), rather than any or all
output from the function. Don't get me wrong: "eval { ... }" is
useful, and I do use this.
B. die()'s return value is going to be 1 in most, if not all, cases,
not whatever I want to return. Unless there's a way to return a
different value; setting $! didn't seem to work.