WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Intermediate · question 39 of 100

How do you perform basic process management in Perl, such as executing external commands or managing child processes?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

Perl provides several ways to perform basic process management, including executing external commands and managing child processes. Here are some examples:

Executing External Commands: ————————– The simplest way to execute an external command from Perl is to use the backtick operator or ‘qx()‘ function. For example:

my $output = `ls -l /tmp`;
print $output;

This will execute the ‘ls -l /tmp‘ command and capture its output to the ‘$output‘ variable, which will be printed to the screen.

Another way to execute external commands is to use the ‘system‘ function. This function takes a command string as its argument and executes it, returning the exit status of the command. For example:

my $exit_status = system("ls -l /tmp");
print $exit_status;

This will execute the ‘ls -l /tmp‘ command and print its exit status to the screen.

Managing Child Processes: ———————— Perl provides several modules for managing child processes, including ‘fork‘, ‘waitpid‘, and ‘kill‘.

The ‘fork‘ function creates a new process by duplicating the current process, creating a parent process and a child process. The child process is an exact copy of the parent process, including all open file descriptors, and starts executing immediately after the fork call. The parent process receives the child process ID (‘$pid‘) as the return value of the fork call, while the child process receives ‘0‘.

For example:

my $pid = fork();
if ($pid == 0) {
    # Child process code
} else {
    # Parent process code
}

The ‘waitpid‘ function waits for a specific child process to finish and returns its exit status. For example:

my $pid = fork();
if ($pid == 0) {
    # Child process code
    exit(42);
} else {
    # Parent process code
    my $child_status = waitpid($pid, 0);
    print "Child process exited with status $child_statusn";
}

This will execute the child process and wait for it to finish, capturing its exit status.

The ‘kill‘ function sends a signal to a specific process. For example:

kill('INT', $pid); # Send an interrupt signal to the child process

This will send an interrupt signal to the child process with the specified process ID (‘$pid‘).

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics