[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Redirecting a print statement to a scalar variable

Posted on 2002-05-02
9
Medium Priority
?
705 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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 400 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

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.

Question has a verified solution.

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

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

656 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