Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing to multiple file handles at once 1

Status
Not open for further replies.

nychris

MIS
Dec 4, 2004
103
US
In my script I open two file handles, one is a pipe to sendmail to send email, and the other is a file handle to a local file on the system. How can I print to both of these at the same time?

Code:
$sendmail = "/usr/sbin/sendmail -t";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
open(FILE, "<fileXYZ") or die "Cannot open fileXYZ for writing: $!\n";

Is there anything better than this?

Code:
print SENDMAIL "foo\n";
print FILE "foo\n";
 
What is print2both? I googled it and nothing came up.
 
I meant to write your own function, something like this:

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]print2both[/maroon] [red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] SENDMAIL [blue]@_[/blue][red];[/red]
        [black][b]print[/b][/black] FILE [blue]@_[/blue][red];[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] SENDMAIL,[red]"[/red][purple]>nychris.sendmail[/purple][red]"[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url][red];[/red]
[black][b]open[/b][/black] FILE,[red]"[/red][purple]>nychris.file[/purple][red]"[/red] or [black][b]die[/b][/black][red];[/red]

print2both [red]"[/red][purple]some[/purple][red]"[/red],[red]"[/red][purple]text[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
print2both [red]"[/red][purple]some more text[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top