LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-09-2006, 09:43 AM   #1
barkers
Member
 
Registered: Apr 2002
Distribution: Debian
Posts: 50

Rep: Reputation: 15
Perl File Handle Problem


Hi,

I have a script in which i need to capture everything written to STDOUT.
I have managed to redirect output from STDOUT to $varaible.
But I cant switch STDOUT back.

How can I do this.


Originally I did this


my $out = new IO::Scalar \$variable;
my $stdout = select $out;
...
close select $stdout;

However one of the packages I was using now does print STDOUT where it used to just print;

Any ideas??

Cheers
 
Old 02-10-2006, 02:20 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Take a look at the example in
man perlfunc
under the function "open".
 
Old 02-10-2006, 03:06 AM   #3
barkers
Member
 
Registered: Apr 2002
Distribution: Debian
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks, however...
I've read it over and over.
It shows you how to store STDOUT in a variable and how to reopen it to a scalar.
But it doesnt show how to reassign STDOUT from the saved variable, so that normal output is resumed.
 
Old 02-10-2006, 03:30 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
In my man page, it explains that (summarized):
open $oldout, ">&STDOUT" #opens STDOUT in $oldout variable
open STDOUT, '>', "foo.out" #redirects STDOUT to "foo.out" (write mode)
open STDOUT, ">&", $oldout #redirect STDOUT once more, this time to $oldout, where the original stdout was
#stored in. So this should effectively restore the normal STDOUT.

Have you tried that?
And added some prints to test if all is well?
 
Old 02-10-2006, 06:43 AM   #5
barkers
Member
 
Registered: Apr 2002
Distribution: Debian
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks,

A couple of differneecs:

I was doing :
my $stdout = *STDOUT;

Instead of:
open $oldout, ">&STDOUT";


Needed a close(STDOUT), but then worked fine.


Cheers
 
Old 02-10-2006, 08:00 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
I suppose you were looking at
man perldata
in the section on filehandles and wrote
my $stdout = *STDOUT; based on that?

Have you also read
man perlfaq5 ?

Functions like "fdopen", "fdclose" (close filehandles based on their number - I think STDOUT is number 1) can be helpful too.

Finally it's also possible to "dup" (duplicate) or alias a filehandle. man perlfaq5 explains how this works, at least for STDIN.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl file handle Xris718 Programming 7 06-06-2005 02:34 PM
problem with writing into a file using socket(perl) akaash Programming 3 04-08-2004 06:06 AM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
how to handle *.img file? nutecinc Linux - Newbie 2 01-28-2003 11:38 AM


All times are GMT -5. The time now is 11:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration