Solved

Redirecting a print statement to a scalar variable

Posted on 2002-05-02
9
658 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
Comment Utility
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
Comment Utility
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
Comment Utility
paulca,

Thank you for taking the time to close 1 & delete the other 2 questions.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
open F,"-|" or &test,exit;
$variable = join'',<F>;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:paulca
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
#!/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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now