paulca
asked on
Redirecting a print statement to a scalar variable
I have a subroutine that performs a print statement as its last command as in the following:
sub test {
# ...
print "how can I capture this in a variable?\n";
}
I am not allowed to change the subroutine. Is there a way to capture the information in the print statement into a variable when I call the test subroutine?
Thanks.
sub test {
# ...
print "how can I capture this in a variable?\n";
}
I am not allowed to change the subroutine. Is there a way to capture the information in the print statement into a variable when I call the test subroutine?
Thanks.
sub test {
# ...
print "how can I capture this in a variable?\n";
}
open F,"-|" or &test;
$variable = join'',<F>;
# ...
print "how can I capture this in a variable?\n";
}
open F,"-|" or &test;
$variable = join'',<F>;
paulca,
Thank you for taking the time to close 1 & delete the other 2 questions.
Thank you for taking the time to close 1 & delete the other 2 questions.
open F,"-|" or &test,exit;
$variable = join'',<F>;
$variable = join'',<F>;
ASKER
This is the error I get when I tried ozo's comment:
'-' is not recognized as an internal or external command,
operable program or batch file.
'-' is not recognized as an internal or external command,
operable program or batch file.
This code works using 5.005_03 on AIX
sub test
{
print " Hello \n";
}
open ( F , "-|") || &test;
while ($line = <F> )
{
print " $line ";
}
Note the diffrence '( )'
open ( F , "-|") || &test;
if I dont use '( )' then it does not work.
ASKER
Sorry but I should have stated this earlier the version of Perl and the OS that I am running:
Perl: 5.005_02 built for MSWin32-x86
OS: Windows 2000
Thanks.
Perl: 5.005_02 built for MSWin32-x86
OS: Windows 2000
Thanks.
#!/usr/bin/perl
use strict;
sub test {
print "Hello\n";
}
open SAVEOUT, ">&STDOUT";
open STDOUT, ">foo.out" or die "Can't redirect stdout";
test();
close STDOUT;
open STDOUT, ">&SAVEOUT";
open FILE,"foo.out";
my @text=<FILE>;
close(FILE);
print "Captured text:" . join('',@text);
use strict;
sub test {
print "Hello\n";
}
open SAVEOUT, ">&STDOUT";
open STDOUT, ">foo.out" or die "Can't redirect stdout";
test();
close STDOUT;
open STDOUT, ">&SAVEOUT";
open FILE,"foo.out";
my @text=<FILE>;
close(FILE);
print "Captured text:" . join('',@text);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You have the following 3 questions open for some time now. Please take some time to review & close them.
EE userid paulca
Total questions asked 87 (100%)
Open questions 4 (4.60%)
Topic Area URL Date Points
MS-SQL https://www.experts-exchange.com/jsp/qShow.jsp?ta=mssql&qid=20284854 04/04/02 100
Perl https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20273591 03/05/02 100
Printers https://www.experts-exchange.com/jsp/qShow.jsp?ta=printers&qid=20009176 12/20/00 50
Your help in closing these questions will be highly appreciated.
Thanks!!
maneshr
(NOT a moderator at EE).