Solved

Redirecting a print statement to a scalar variable

Posted on 2002-05-02
9
678 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

786 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