develooper Front page | perl.beginners | Postings from January 2002

Re: Standard FileHandles for STDIN, STDOUT, STDERR

Thread Previous | Thread Next
From:
Jenda Krynicky
Date:
January 30, 2002 08:57
Subject:
Re: Standard FileHandles for STDIN, STDOUT, STDERR
Message ID:
3C583433.16313.18304AB3@localhost
From:           	Adriano Rodrigues Ferreira <ferreira@triang.com.br>

> When using FileHandles as the object-oriented wrapper for file
> handles, are there predefined handles which can be used to get the
> standard input, standard or standard error files?
> 
> If they are not predefined, how I can create them?
> 
> Best regards,
> 
> Adriano.

You can do this:

	my $FH = *STDOUT;
	print $FH "Hello World\n";

which creates an ALIAS for STDIN, so if you later redirect the 
STDOUT somewhere the $FH will "get redirected" too:

	#!perl
	use FileHandle;

	$FH = *STDOUT;
	print $FH "Hello World\n";

	open STDOUT, '>c:\temp\zkOUT.txt';
	print STDOUT "This goes to STDOUT\n";
	print $FH "And this goes to \$FH\n";
	__END__

As you can see both the "this goes to ..." messages end up in the 
file.

Or you can use :

	open $FH, '>&STDOUT';

which dup()s the filehandle so that even if you change where 
STDOUT points to, the $FH will still write to the original 
file/device/pipe.

	#!perl
	use FileHandle;

	open $FH, '>&STDOUT';
	print $FH "Hello World\n";

	open STDOUT, '>c:\temp\zkOUT.txt';
	print STDOUT "This goes to STDOUT\n";
	print $FH "And this goes to \$FH\n";
	__END__

HTH, Jenda

=========== Jenda@Krynicky.cz == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
					--- me

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About