Portál AbcLinuxu, 25. listopadu 2025 08:10
Perl jen system... - exec se uz "nikdy nevrati do Perlu".
Ale jinak se mi to zda dobre - jen na Perlu nesnasim to $_ - pak clovek musi u kodu "moc premyslet", co v tom ma...
open (MTAB, "/etc/mtab") or die "Could not open mtab: $!";
foreach (<MTAB>) {
system($_);
# nebo
# `$_`;
}
close MTAB;
Problem neni v jazyku, je to někde jinde...
exec se v "Perlu" chová stejně jako v "céčku" (a myslím si, že i v "Pythonu"). Ostatně man perlfunc vypíše toto: The "exec" function executes a system command and never returns-- use "system" instead of "exec" if you want it to return.
exec (včetně jeho ostatních variant) se dá použít v případě, že se nejdříve vytvoří další proces a ten (potomek) se přepíše. Viz. dokumentace k fork.
Ostatně system to dělá přesně tak. V souvislosti s ním ještě zbývá zmínit další volání jádra waitpid.
$perldoc -f exec
exec PROGRAM LIST
The "exec" function executes a system command and never
returns-- use "system" instead of "exec" if you want it to
return. ...
$perldoc -f system
system LIST
system PROGRAM LIST
Does exactly the same thing as "exec LIST", except that a fork
is done first, and the parent process waits for the child pro-
cess to complete. ...
$pydoc os.execve
os.execve = execve(...)
execve(path, args, env)
Execute a path with arguments and environment, replacing current process.
...
$pydoc os.sysem
os.system = system(...)
system(command) -> exit_status
Execute the command (a string) in a subshell.
$man execve
NAME
execve - execute program
SYNOPSIS
#include <unistd.h>
int execve(const char *filename, char *const argv [], char *const
envp[]);
DESCRIPTION
execve() executes the program pointed to by filename.
...
execve() does not return on success, and the text, data, bss, and stack
of the calling process are overwritten by that of the program loaded.
The program invoked inherits the calling process's PID, ...
$man system
NAME
system - execute a shell command
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed. ...
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.