# define STATUS_ALL_FAILURE (PL_statusvalue = 1)
#endif
+/* This defines a way to flush all output buffers. This may be a
+ * performance issue, so we allow people to disable it.
+ * XXX the default needs a Configure test, as it may not work everywhere.
+ */
+#ifndef PERL_FLUSHALL_FOR_CHILD
+#define PERL_FLUSHALL_FOR_CHILD PerlIO_flush((PerlIO*)NULL)
+#endif
+
/* Some unistd.h's give a prototype for pause() even though
HAS_PAUSE ends up undefined. This causes the #define
below to be rejected by the compmiler. Sigh.
the replacement expression in C<eval 's/.../.../e'>. This has
been fixed.
+=head2 Automatic flushing of output buffers
+
+fork(), exec(), system(), qx// and pipe open()s now flush the buffers
+of all files that were opened for output at the time the operation
+was attempted. The mostly eliminates the often confusing effects of
+buffering mishaps suffered by users unaware of how Perl internally
+handled I/O.
+
=head1 Supported Platforms
=over 4
the entire argument is passed to the system's command shell for parsing
(this is C</bin/sh -c> on Unix platforms, but varies on other platforms).
If there are no shell metacharacters in the argument, it is split into
-words and passed directly to C<execvp()>, which is more efficient. Note:
-C<exec()> and C<system()> do not flush your output buffer, so you may need to
-set C<$|> to avoid lost output. Examples:
+words and passed directly to C<execvp()>, which is more efficient.
- exec '/bin/echo', 'Your arguments are: ', @ARGV;
- exec "sort $outfile | uniq";
+All files opened for output are flushed before attempting the exec().
If you don't really want to execute the first argument, but want to lie
to the program you are executing about its own name, you can specify
example, using copy-on-write technology on data pages), making it the
dominant paradigm for multitasking over the last few decades.
-Note: unflushed buffers remain unflushed in both processes, which means
-you may need to set C<$|> ($AUTOFLUSH in English) or call the C<autoflush()>
-method of C<IO::Handle> to avoid duplicate output.
+All files opened for output are flushed before forking the child process.
If you C<fork()> without ever waiting on your children, you will
accumulate zombies. On some systems, you can avoid this by setting
See L<perlipc/"Safe Pipe Opens"> for more examples of this.
-NOTE: On any operation that may do a fork, any unflushed buffers remain
-unflushed in both processes, which means you may need to set C<$|> to
-avoid duplicate output. On systems that support a close-on-exec flag on
-files, the flag will be set for the newly opened file descriptor as
-determined by the value of $^F. See L<perlvar/$^F>.
+NOTE: On any operation that may do a fork, all files opened for output
+are flushed before the fork is attempted. On systems that support a
+close-on-exec flag on files, the flag will be set for the newly opened
+file descriptor as determined by the value of $^F. See L<perlvar/$^F>.
Closing any piped filehandle causes the parent process to wait for the
child to finish, and returns the status value in C<$?>.
Both the main process and any child processes it forks share the same
STDIN, STDOUT, and STDERR filehandles. If both processes try to access
-them at once, strange things can happen. You'll certainly want to any
-stdio flush output buffers before forking. You may also want to close
+them at once, strange things can happen. You may also want to close
or reopen the filehandles for the child. You can get around this by
opening your pipe with open(), but on some systems this means that the
child process cannot outlive the parent.
use FileHandle;
use IPC::Open2;
$pid = open2(*Reader, *Writer, "cat -u -n" );
- Writer->autoflush(); # default here, actually
print Writer "stuff\n";
$got = <Reader>;
GV *tmpgv;
EXTEND(SP, 1);
+ PERL_FLUSHALL_FOR_CHILD;
childpid = fork();
if (childpid < 0)
RETSETUNDEF;
TAINT_PROPER("system");
}
}
+ PERL_FLUSHALL_FOR_CHILD;
#if (defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(OS2)
while ((childpid = vfork()) == -1) {
if (errno != EAGAIN) {
I32 value;
STRLEN n_a;
+ PERL_FLUSHALL_FOR_CHILD;
if (PL_op->op_flags & OPf_STACKED) {
SV *really = *++MARK;
value = (I32)do_aexec(really, MARK, SP);
SV *sv;
I32 doexec = strNE(cmd,"-");
+ PERL_FLUSHALL_FOR_CHILD;
#ifdef OS2
if (doexec) {
return my_syspopen(cmd,mode);
#if defined(atarist) || defined(DJGPP)
FILE *popen();
PerlIO *
-my_popen(cmd,mode)
-char *cmd;
-char *mode;
+my_popen(char *cmd, char *mode)
{
/* Needs work for PerlIO ! */
/* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
+ PERL_FLUSHALL_FOR_CHILD;
return popen(PerlIO_exportFILE(cmd, 0), mode);
}
#endif
Perl_stdin_fd = pFd[that];
if (strNE(cmd,"-"))
{
+ PERL_FLUSHALL_FOR_CHILD;
pid = spawn_cmd(cmd, Perl_stdin_fd, Perl_stdout_fd);
if (pid >= 0)
{
{
TAINT_ENV();
TAINT_PROPER("popen");
+ PERL_FLUSHALL_FOR_CHILD;
return safe_popen(cmd,mode);
}
#define fixcmd(x)
#endif
fixcmd(cmd);
- win32_fflush(stdout);
- win32_fflush(stderr);
+ PERL_FLUSHALL_FOR_CHILD;
return win32_popen(cmd, mode);
}