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 7879523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:54:48+00:00 2026-06-03T03:54:48+00:00

I’m a beginner in Perl. I have a Windows batch script which contains multiple

  • 0

I’m a beginner in Perl. I have a Windows batch script which contains multiple NMake commands. An existing issue with this batch script is that even if the NMake command fails during its execution, ERRORLEVEL doesn’t get set properly.

So we never know whether the command worked until we parse the log file. I looked into it but couldn’t find a solution. I, then thought of converting this batch script to a Perl script assuming that trapping error will be easier but it seems it’s not that easy 🙂

Whenever I run my Perl script, the ‘system’ command always returns 0. I looked at many different links, and realized that capturing the correct return status of ‘system’ command is not that straightforward. Still, I tried the suggestions but things are not working. 🙁

Let me mention that the NMake command that is called, in turn, calls many different commands during its execution. For instance, the command output mentioned below, which is throwing ‘fatal error’, is actually part of a Perl script (check_dir.pl). This call to Perl script is written in the NMake file itself.

If I call this Perl file (check_dir.pl) directly and check for exit value, I get correct result i.e., the command fails and prints a non-zero exit value (…unexpectedly returned exit value 2).

Tried Perl’s system function but it didn’t help. I used the following code:

system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1");

if ( $? == -1 ) {
    print "Command failed to execute: $!\n";
}
elsif ( $? & 127 ) {
    printf "The child died with signal %d, %s a coredump\n",
    ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
}
else {
    printf "child exited with value %d\n", $? >> 8;
}

Output:

.....  
.....  
Unable to open dir: R:\TSM_Latest  
Compressing...NMAKE : fatal error U1077: 'if' : return code '0x2'  
Stop.  
child exited with value 0

Also tried:

use IPC::System::Simple qw(system);  
my $exit_status = system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1");

if ($exit_status != 0) {  
    print "Failure";  
    exit 3;  
} else {  
    print "Success";  
}

Finally tried the following module:

use IPC::Run qw( run timeout );  
run "nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1" or die "NMake returned $?";

Nothing seems to be working 🙁

Please correct me if i’m interpreting the return value of system incorrectly.

  • 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-06-03T03:54:49+00:00Added an answer on June 3, 2026 at 3:54 am

    You have:

    use IPC::System::Simple qw(system);
    my $exit_status = system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1");
    

    Given that you don’t seem to care about the actual output, you can try

    my $exit_status = systemx(nmake => 
                                  qw(/f _nt.mak pack_cd),
                                  "SUB_PLAT=$PLAT",
                                  "DR=$plat",
                              );
    

    To make sure you bypass cmd.exe and see if you get something useful.

    For reference, the exit codes from nmake are listed here.

    Running the following program:

    use strict; use warnings;
    
    use IPC::System::Simple qw(systemx);
    use Try::Tiny;
    
    my $status = 0;
    
    try   { systemx nmake => qw(/f bogus) }
    catch { ($status) = ( /exit value ([0-9])/ ) };
    
    print "Failed to execute nmake. Exit status = $status\n";
    

    produces:

    NMAKE : fatal error U1052: file 'bogus' not found
    Stop.
    Failed to execute nmake. Exit status = 2

    The following version:

    use strict; use warnings;
    
    my $status = system nmake => qw(/f bogus);
    
    if ($status) {
        if ($? == -1) {
            print "failed to execute: $!\n";
        }
        elsif ($? & 127) {
            printf "child died with signal %d, %s coredump\n",
            ($? & 127), ($? & 128) ? 'with' : 'without';
        }
        else {
            printf "child exited with value %d\n", $? >> 8;
        }
    }
    

    produces:

    NMAKE : fatal error U1052: file 'bogus' not found
    Stop.
    child exited with value 2

    In fact, even when I use

    my $status = system "nmake /f bogus";
    

    I get the same correct and expected output.

    Ditto when I use

    my $status = system "nmake /f bogus 2>&1";
    

    These observations lead me to the following questions:

    1. Which version of nmake are you using?

    2. Is the /I option in effect? Even though you don’t set it from the command line, note the following:

    /I Ignores exit codes from all commands. To set or clear /I for part of a makefile, use !CMDSWITCHES. To ignore exit codes for part of a makefile, use a dash (–) command modifier or .IGNORE. Overrides /K if both are specified.

    So, I put together the following files:

    C:\temp> cat test.mak
    test.target: bogus.pl; perl bogus.pl
    C:\temp> cat bogus.pl
    exit 1;

    And, ran:

    use strict; use warnings;
    
    my $status = system "nmake /f test.mak 2>&1";
    
    if ($status) {
        if ($? == -1) {
            print "failed to execute: $!\n";
        }
        elsif ($? & 127) {
            printf "child died with signal %d, %s coredump\n",
            ($? & 127), ($? & 128) ? 'with' : 'without';
        }
        else {
            printf "child exited with value %d\n", $? >> 8;
        }
    }
    

    which gave me the output:

            perl bogus.pl
    NMAKE : fatal error U1077: 'c:\opt\perl\bin\perl.EXE' : return code '0x1'
    Stop.
    child exited with value 2

    where the last line shows that the exit status of nmake was correctly propagated.

    Conclusion:

    You have some other problem.

    In fact, the OP later pointed out in comments that:

    The actual command that i am trying to run is: system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1 | C:\\tee2 $TEMP_DIR\\modules-nt_${platlogfile}");

    Given tees involvement in the pipeline, it is not surprising that nmakes exit code gets lost. tee is successfully able to process output from nmake, so it returns success, and that’s the exit code your script sees.

    Therefore, the solution is to capture the output of nmake yourself, either using qx (coupled with the appropriate level of error checking), or using capture from IPC::System::Simple. Then, you can decide to whether you want to print that output, save to a file, put it in an email etc …

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.