Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6604909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:13:12+00:00 2026-05-25T19:13:12+00:00

I am currently modifying a Perl script that executes several shell commands, and, for

  • 0

I am currently modifying a Perl script that executes several shell commands, and, for some of them, capture the output for further processing. I want to factor out the code that executes the external commands in subroutines. I wrote the following subroutines:

sub execute_command {
  my $cmd = shift;

  Log("executing command $cmd ...");
  system($cmd);
  my $app = ( $? == -1 ) ? $? : $? >> 8;
  if ($app != 0) {
    Log("error executing command $cmd");
    return $FAILURE;
  }
  Log("done");
  return $SUCCESS;
}

sub execute_command_and_get_output {
  my $cmd = shift;

  Log("executing command $cmd ...");
  unless (open( CMD, "$cmd|" )) {
    Log("error executing command $cmd");
    return undef;
  }
  my @cmd = <CMD>;
  close(CMD);
  Log("done");
  return @cmd;    
}

Questions:

  1. execute_command should execute the passed command and return $SUCCESS if everything is OK, or $FAILURE in case of errors. Am I testing $? in the right way?

  2. execute_command_and_get_output should execute the passed command and return the output as an array (containing the output lines); if the execution of the command fails, it should return undef. Is it correct to use unless (open( CMD, "$cmd|" )) { ... } to test for error conditions in the command execution?

Besides answers to my two questions, any suggestion for improvement is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:13:12+00:00Added an answer on May 25, 2026 at 7:13 pm

    1) If you don’t care what error has happened, it is enough to check return value of system:

    if(system($cmd) == 0) { return $SUCCESS } else { return $FAILURE }
    

    2) Instead of return undef, it is usually better to just return. It works better when called in list context (it is still false). If the return is large, you might want to return reference to avoid copying.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, if I want to output a SQL script for a table in my
I have a small awk script that does some in-place file modifications (to a
I'm currently modifying a class that has 9 different constructors. Now overall I believe
I am currently modifying an existing system that generates reports with iText and java.
i'm currently modifying a legacy application that uses the SqlHelper class in c#. how
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Currently, I am developing a product that does fairly intensive calculations using MS SQL
I'm modifying a pre-existing script in Xcode to customize my file headers. The script
I'm modifying a mature CGI application written in Perl and the question of content
I'm currently modifying a document literal SOAP service for a business app which transfers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.