The question is a bit vague, I assume you mean to do something like this:
#!/usr/bin/perl -w
use strict;
my ($num1,$num2);
$num1=2;
$num2=sum($num1,$num1);
print "$num2\n";
# Sum: returns the sum of all arguments.
sub sum {
my $ret;
$ret=0;
for(@_){$ret+=$_}
return $ret;
}
return tells perl what the subroutine should give back when called, so $num2 is given the final value of $ret in the subroutine sum. | [reply] [Watch: Dir/Any] [d/l] [select] |
Do you mean if the function print's to STDOUT?
You could maybe use local to temporarily turn
STDOUT into a tied IO::Scalar filehandle...haven't tried it myself but worth a try. | [reply] [Watch: Dir/Any] |
You can do it with a pipe-open, as described
in man perlipc.
#!/usr/bin/perl
use strict;
use warnings 'all';
sub my_function {
# Does a lot, then writes
print "Foobar!";
}
my $pid = open my $kid => "-|";
die "Failed to fork: $!\n" unless defined $pid;
unless ($pid) {
my_function;
exit;
}
my $result;
{
local $/;
$result = <$kid>;
close $kid or die "Failed to close pipe: $!\n";
}
print "Got: $result\n";
Abigail | [reply] [Watch: Dir/Any] [d/l] [select] |
As a general rule functions should always return a value not print that value out. Following this rule will avoid problems like this.
But if you don't have the option of modifying the function you'll have to do some trickery with STDOUT. | [reply] [Watch: Dir/Any] |