|| die "can't connect to port 80 on www.perl.com: $!";
$handle->autoflush(1);
if (fork()) { # XXX: undef means failure
select($handle);
print while <STDIN>; # everything from stdin to socket
} else {
print while <$handle>; # everything from socket to stdout
}
close $handle;
exit;
=head2 How can I write expect in Perl?
Once upon a time, there was a library called chat2.pl (part of the
standard perl distribution), which never really got finished. If you
find it somewhere, I<don't use it>. These days, your best bet is to
look at the Expect module available from CPAN, which also requires two
other modules from CPAN, IO::Pty and IO::Stty.
=head2 Is there a way to hide perl's command line from programs such as "ps"?
First of all note that if you're doing this for security reasons (to
avoid people seeing passwords, for example) then you should rewrite
your program so that critical information is never given as an
argument. Hiding the arguments won't make your program completely
secure.
To actually alter the visible command line, you can assign to the
variable $0 as documented in L<perlvar>. This won't work on all
operating systems, though. Daemon programs like sendmail place their
state there, as in:
$0 = "orcus [accepting connections]";
=head2 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?
=over 4
=item Unix
In the strictest sense, it can't be done--the script executes as a
different process from the shell it was started from. Changes to a
process are not reflected in its parent--only in any children
created after the change. There is shell magic that may allow you to
fake it by eval()ing the script's output in your shell; check out the
comp.unix.questions FAQ for details.
=back
=head2 How do I close a process's filehandle without waiting for it to complete?
Assuming your system supports such things, just send an appropriate signal
to the process (see L<perlfunc/"kill">). It's common to first send a TERM
signal, wait a little bit, and then send a KILL signal to finish it off.
=head2 How do I fork a daemon process?
If by daemon process you mean one that's detached (disassociated from
its tty), then the following process is reported to work on most
Unixish systems. Non-Unix users should check their Your_OS::Process
module for other solutions.
=over 4
=item *
Open /dev/tty and use the TIOCNOTTY ioctl on it. See L<tty>
for details. Or better yet, you can just use the POSIX::setsid()
function, so you don't have to worry about process groups.
=item *
Change directory to /
=item *
Reopen STDIN, STDOUT, and STDERR so they're not connected to the old
tty.
=item *
Background yourself like this:
fork && exit;
=back
The Proc::Daemon module, available from CPAN, provides a function to
perform these actions for you.
=head2 How do I find out if I'm running interactively or not?
Good question. Sometimes C<-t STDIN> and C<-t STDOUT> can give clues,
sometimes not.
if (-t STDIN && -t STDOUT) {
print "Now what? ";
}
On POSIX systems, you can test whether your own process group matches
the current process group of your controlling terminal as follows:
use POSIX qw/getpgrp tcgetpgrp/;
open(TTY, "/dev/tty") or die $!;
$tpgrp = tcgetpgrp(fileno(*TTY));
$pgrp = getpgrp();
if ($tpgrp == $pgrp) {
print "foreground\n";
} else {
print "background\n";
}
=head2 How do I timeout a slow event?
Use the alarm() function, probably in conjunction with a signal
handler, as documented in L<perlipc/"Signals"> and the section on
``Signals'' in the Camel. You may instead use the more flexible
Sys::AlarmCall module available from CPAN.
The alarm() function is not implemented on all versions of Windows.
Check the documentation for your specific version of Perl.
=head2 How do I set CPU limits?
Use the BSD::Resource module from CPAN.
=head2 How do I avoid zombies on a Unix system?
Use the reaper code from L<perlipc/"Signals"> to call wait() when a
SIGCHLD is received, or else use the double-fork technique described
in L<perlfaq8/"How do I start a process in the background?">.
=head2 How do I use an SQL database?
The DBI module provides an abstract interface to most database
servers and types, including Oracle, DB2, Sybase, mysql, Postgresql,
ODBC, and flat files. The DBI module accesses each database type
through a database driver, or DBD. You can see a complete list of
available drivers on CPAN: http://www.cpan.org/modules/by-module/DBD/ .
You can read more about DBI on http://dbi.perl.org .
Other modules provide more specific access: Win32::ODBC, Alzabo, iodbc,
and others found on CPAN Search: http://search.cpan.org .
=head2 How do I make a system() exit on control-C?
You can't. You need to imitate the system() call (see L<perlipc> for
sample code) and then have a signal handler for the INT signal that
passes the signal on to the subprocess. Or you can check for it:
$rc = system($cmd);
if ($rc & 127) { die "signal death" }
=head2 How do I open a file without blocking?
If you're lucky enough to be using a system that supports
non-blocking reads (most Unixish systems do), you need only to use the
O_NDELAY or O_NONBLOCK flag from the Fcntl module in conjunction with