Re: How to collect the stdout of a subroutine call?
by b10m (Vicar) on Apr 19, 2004 at 18:12 UTC
|
my $variable = results();
sub results {
# ... do something that will give $foo the stuff you want
# to return ...
return $foo;
}
--
b10m
All code is usually tested, but rarely trusted.
| [reply] [d/l] |
Re: How to collect the stdout of a subroutine call?
by fletcher_the_dog (Friar) on Apr 19, 2004 at 18:14 UTC
|
I would suggest trying your idea and seeing if it works.
sub results {
return
"Name:xxx
Address: Main street
City: Boston
Phone: 123 234 2345"
}
$variable = &results();
print $variable;
| [reply] [d/l] |
|
Here is the code but it still doesn't work.
my $xx = &log;
print "<b>$xx</x>";
Don't know why???
sub log
{
my $log_file = "log.txt";
open(LOGFILE, "$log") or die("Could not open log file.");
#my %seen;
my %seen = ();
my %typeseen = ();
# read it in
my $test = "Home Company";
while(<LOGFILE>)
{
next unless /($test)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.*?)\*(.
+*?)$/;
my ($item, $logintype) = ($1, $4);
#print $item;
$seen{$item}++;
$typeseen{$item}{$logintype}++;
}
# dump it out
foreach my $item (sort keys %seen)
{
print "$item:<br>\n";
print "\tLog in activity: ", $seen{$item}, "<br>\n";
foreach my $type (sort keys %{$typeseen{$item}})
{
print "\t", $type, " activity: ", $typeseen{$item}{$type}, "<br>\n
+";
}
}
} ####### END SUB LOG
| [reply] [d/l] [select] |
|
| [reply] |
|
If you don't want to change the code in the subroutine to accumulate and return a string, instead of printing, you can try the following. Similar but prettier techniques use non-standard modules.
# somewhere near the top of your code, put this:
{
package AccumHandle;
sub TIEHANDLE { my( $pkg, $s ) = shift; bless \$s, $pkg }
sub PRINT { my $self = shift; $$self .= join($,,@_).$\; }
}
# now do this where you call the subroutine:
local ACC;
tie *ACC, 'AccumHandle';
select ACC;
&log();
select STDOUT;
my $xx = ${tied(*ACC)};
print "<b>$xx</x>";
Btw - this question is a (currently unapproved) Categorized Question: How can I write a function's output to a scalar?.
jdporter The 6th Rule of Perl Club is -- There is no Rule #6.
| [reply] [d/l] |
|
You must use the return statement inside your subroutine. I am not sure I understand exactly what you are trying to do from reading your code, but I am assuming you want everything you are printing to be returned (Update and not printed). In that case you need to put the data in the variable first, then return it. Like this.... (code not tested)
my $output;
foreach my $item (sort keys %seen)
{
$output .= "$item:<br>\n";
$output .= "\tLog in activity: ", $seen{$item}, "<br>\n";
foreach my $type (sort keys %{$typeseen{$item}})
{
$output .= "\t", $type, " activity: ", $typeseen{$item}{$type}, "<
+br>\n";
}
return $output;
}
| [reply] [d/l] |
|
|
|
|
|
| [reply] [d/l] [select] |
Re: How to collect the stdout of a subroutine call?
by disciple (Pilgrim) on Apr 19, 2004 at 18:35 UTC
|
If you want to return the values into individual variables....
($name, $address, $city, $phone) = &getInfo();
print "Name: $name\nAddress: $address\nCity: $city\nPhone: $phone";
sub getInfo(){
return ("xxx", "Main street", "Boston", "123 234 2345");
}
| [reply] [d/l] |
Re: How to collect the stdout of a subroutine call?
by Aragorn (Curate) on Apr 19, 2004 at 18:50 UTC
|
You can return the results as a string, but (and I'm guessing here) I think it would be more useful if the results are returned as a hash reference:
sub results {
# Guessing that the results are in separate variables $name,
# $address, $city, $phone:
return {
name => $name,
address => $address,
city => $city,
phone => $phone,
};
}
Now you can get to the separate entries of the results in your main program:
my $info = results();
print "Name: " . $info->{name} . "\n";
# And of course $info->{address}, $info->{city} and $info->{phone}
Take a look at perldsc and perlref for more information on how these things work.
Arjen
| [reply] [d/l] [select] |
Re: How to collect the stdout of a subroutine call?
by ysth (Canon) on Apr 19, 2004 at 22:47 UTC
|
With perl5.8 and above, you can easily capture what the
subroutine prints like:
# create a variable to store the output in
my $output = "";
# open a "file" that will store output in the variable
# (requires 5.8; before that use CPAN module IO::Scalar)
open my $fh, ">", \$output or die "Unexpected open to scalar failure:
+$!";
# save the default output file handle
my $select_save = select($fh);
# call your function
call_function();
# clean up
select($select_save);
close $fh;
# use output
print "Got output: $output\n";
| [reply] [d/l] |