Solved

Redirecting a print statement to a scalar variable

Posted on 2002-05-02
9
691 Views
Last Modified: 2008-01-09
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
Comment
Question by:paulca
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6987462
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
 
LVL 84

Expert Comment

by:ozo
ID: 6987572
sub test {
        # ...

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

Expert Comment

by:maneshr
ID: 6987576
paulca,

Thank you for taking the time to close 1 & delete the other 2 questions.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 6988073
open F,"-|" or &test,exit;
$variable = join'',<F>;
0
 

Author Comment

by:paulca
ID: 6988464
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
 
LVL 1

Expert Comment

by:sushrut
ID: 6988655

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
 

Author Comment

by:paulca
ID: 6988670
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
 
LVL 10

Expert Comment

by:rj2
ID: 6988932
#!/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
 
LVL 10

Accepted Solution

by:
rj2 earned 100 total points
ID: 6988935
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cpan issue 1 66
perl to convert excel to csv 3 310
Convert grep lines to perl 6 80
get the last nth lines from a an array if nth element is xxx 2 75
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question