Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

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.
0
paulca
Asked:
paulca
  • 2
  • 2
  • 2
  • +2
1 Solution
 
maneshrCommented:
paulca,

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   http://www.experts-exchange.com/jsp/qShow.jsp?ta=mssql&qid=20284854   04/04/02   100  
Perl   http://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20273591   03/05/02   100  
Printers   http://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).
0
 
ozoCommented:
sub test {
        # ...

        print "how can I capture this in a variable?\n";
 }
open F,"-|" or &test;
$variable = join'',<F>;
0
 
maneshrCommented:
paulca,

Thank you for taking the time to close 1 & delete the other 2 questions.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ozoCommented:
open F,"-|" or &test,exit;
$variable = join'',<F>;
0
 
paulcaAuthor Commented:
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.
0
 
sushrutCommented:

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.

0
 
paulcaAuthor Commented:
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.
0
 
rj2Commented:
#!/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);
0
 
rj2Commented:
Oops, I just noticed you said "scalar".

#!/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: $text"
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now