X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq8.pod;h=f4d3c12f6f73b37b5586635307c9b68b0f9d3d65;hb=1b1626e441fa9750f3953e46530f2e95046bb007;hp=7250afbca5970a000d182c3479eb1c638b29b914;hpb=3fe9a6f19eb206c685bd7389e54e2838fdfd04b7;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq8.pod b/pod/perlfaq8.pod index 7250afb..f4d3c12 100644 --- a/pod/perlfaq8.pod +++ b/pod/perlfaq8.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq8 - System Interaction ($Revision: 1.17 $, $Date: 1997/03/25 18:17:12 $) +perlfaq8 - System Interaction ($Revision: 1.21 $, $Date: 1997/04/24 22:44:19 $) =head1 DESCRIPTION @@ -267,7 +267,7 @@ http://www.perl.com/CPAN/doc/misc/ancient/tutorial/eg/itimers.pl . =head2 How can I measure time under a second? In general, you may not be able to. The Time::HiRes module (available -from CPAN) provides this functionality for some systems. +from CPAN) provides this functionality for some systems. In general, you may not be able to. But if you system supports both the syscall() function in Perl as well as a system call like gettimeofday(2), @@ -381,14 +381,14 @@ documentation, though (see L). =head2 Why can't I get the output of a command with system()? -You're confusing the purpose of system() and backticks (''). system() -runs a command and returns exit status information (as a 16 bit value --- the low 8 bits are the signal the process died from, if any, and -the high 8 bits are the actual exit value). Backticks ('') run a +You're confusing the purpose of system() and backticks (``). system() +runs a command and returns exit status information (as a 16 bit value: +the low 8 bits are the signal the process died from, if any, and +the high 8 bits are the actual exit value). Backticks (``) run a command and return what it sent to STDOUT. - $status = system("mail-users"); - $output = `ls`; + $exit_status = system("mail-users"); + $output_string = `ls`; =head2 How can I capture STDERR from an external command? @@ -513,7 +513,7 @@ You have to do this: Just as with system(), no shell escapes happen when you exec() a list. -=head2 Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MSDOS)? +=head2 Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)? Because some stdio's set error and eof flags that need clearing. The POSIX module defines clearerr() that you can use. That is the @@ -557,9 +557,27 @@ causes many inefficiencies. =head2 Can I use perl to run a telnet or ftp session? -Try the Net::FTP and TCP::Client modules (available from CPAN). -http://www.perl.com/CPAN/scripts/netstuff/telnet.emul.shar will also -help for emulating the telnet protocol. +Try the Net::FTP, TCP::Client, and Net::Telnet modules (available from +CPAN). http://www.perl.com/CPAN/scripts/netstuff/telnet.emul.shar +will also help for emulating the telnet protocol, but Net::Telnet is +quite probably easier to use.. + +If all you want to do is pretend to be telnet but don't need +the initial telnet handshaking, then the standard dual-process +approach will suffice: + + use IO::Socket; # new in 5.004 + $handle = IO::Socket::INET->new('www.perl.com:80') + || die "can't connect to port 80 on www.perl.com: $!"; + $handle->autoflush(1); + if (fork()) { # XXX: undef means failure + select($handle); + print while ; # 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? @@ -643,20 +661,6 @@ Background yourself like this: See the F script (part of the perl source distribution). -=head2 How do I keep my own module/library directory? - -When you build modules, use the PREFIX option when generating -Makefiles: - - perl Makefile.PL PREFIX=/u/mydir/perl - -then either set the PERL5LIB environment variable before you run -scripts that use the modules/libraries (see L) or say - - use lib '/u/mydir/perl'; - -See Perl's L for more information. - =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, @@ -754,7 +758,92 @@ If your version of perl is compiled without dynamic loading, then you just need to replace step 3 (B) with B and you will get a new F binary with your extension linked in. -See L for more details on building extensions. +See L for more details on building extensions, +the question "How do I keep my own module/library directory?" + +=head2 How do I keep my own module/library directory? + +When you build modules, use the PREFIX option when generating +Makefiles: + + perl Makefile.PL PREFIX=/u/mydir/perl + +then either set the PERL5LIB environment variable before you run +scripts that use the modules/libraries (see L) or say + + use lib '/u/mydir/perl'; + +See Perl's L for more information. + +=head2 How do I add the directory my program lives in to the module/library search path? + + use FindBin; + use lib "$FindBin:Bin"; + use your_own_modules; + +=head2 How do I add a directory to my include path at runtime? + +Here are the suggested ways of modifying your include path: + + the PERLLIB environment variable + the PERL5LIB environment variable + the perl -Idir commpand line flag + the use lib pragma, as in + use lib "$ENV{HOME}/myown_perllib"; + +The latter is particularly useful because it knows about machine +dependent architectures. The lib.pm pragmatic module was first +included with the 5.002 release of Perl. + +=head1 How do I get one key from the terminal at a time, under POSIX? + + #!/usr/bin/perl -w + use strict; + $| = 1; + for (1..4) { + my $got; + print "gimme: "; + $got = getone(); + print "--> $got\n"; + } + exit; + + BEGIN { + use POSIX qw(:termios_h); + + my ($term, $oterm, $echo, $noecho, $fd_stdin); + + $fd_stdin = fileno(STDIN); + + $term = POSIX::Termios->new(); + $term->getattr($fd_stdin); + $oterm = $term->getlflag(); + + $echo = ECHO | ECHOK | ICANON; + $noecho = $oterm & ~$echo; + + sub cbreak { + $term->setlflag($noecho); + $term->setcc(VTIME, 1); + $term->setattr($fd_stdin, TCSANOW); + } + + sub cooked { + $term->setlflag($oterm); + $term->setcc(VTIME, 0); + $term->setattr($fd_stdin, TCSANOW); + } + + sub getone { + my $key = ''; + cbreak(); + sysread(STDIN, $key, 1); + cooked(); + return $key; + } + + } + END { cooked() } =head1 AUTHOR AND COPYRIGHT