amitrev has asked for the wisdom of the Perl Monks concerning the following question:

Hi to everyone. The following problem was frustrating me for the last few days. The problem lays in this part of my code
sub returnVersion() { my $cmdToExecute = "dds_cmd -Version "; my $versionNum; my $PH; if (!open($PH, "$cmdToExecute 2>&1 |")) { print STDERR "Failed to execute command:($cmdToExecute)--$!\n"; exit 1; } while (my $line = <$PH>) { if (index($line, "Command Execution ") != -1){ $line =~ s/^.+version\s|\.0.$//g; $versionNum = $line; } } close($PH); return $versionNum; }

So the execution hangs on line "while (my $line = <$PH>) {".

If I execute the "cmdToExecute" manually on cmd works fine-returning 3 lines of text as expected. If I separate this code in separate .pl file and execute it also works fine-and the command returns 3 lines of text.

I don't know why is this happening and why it wont work in my code.

And the strange is that this problem appears only on win2003. On every other platform works fine

Replies are listed 'Best First'.
Re: Execution of external command hangs
by Old_Gray_Bear (Bishop) on Aug 29, 2013 at 23:31 UTC
    Try a different command, say dir. If it works, the the problem dds_cmd itself. (Perhaps it doesn't write an EOL after its output or something else silly like that.)

    When I tried to reproduce your problem I got

    Microsoft Windows Version 6.1.7601 Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\bear>dds_cmd -Version 'dds_cmd' is not recognized as an internal or external command,operable program or batch file.

    C:\Users\bear>

    So, what is the dds_cmd command? And what is it supposed to give me? Does it depend on the rights of the User executing the command? Or perhaps an ENV variable that is set in your profile and not in the profile executing the Perl code?

    ----
    I Go Back to Sleep, Now.

    OGB

      It is exe - C code.

      I have try the sugestion of BrowserUk adding the local $/ = "\n"; ################### line but without success

      The problem is still there and I'm still not able to find why this appears only on Windows 2003

Re: Execution of external command hangs
by BrowserUk (Patriarch) on Aug 29, 2013 at 23:26 UTC

    Does anything in your app set $/?

    Try adding the local line below:

    sub returnVersion() { my $cmdToExecute = "dds_cmd -Version "; my $versionNum; my $PH; if (!open($PH, "$cmdToExecute 2>&1 |")) { print STDERR "Failed to execute command:($cmdToExecute)--$!\n"; exit 1; } local $/ = "\n"; ################### while (my $line = <$PH>) { if (index($line, "Command Execution ") != -1){ $line =~ s/^.+version\s|\.0.$//g; $versionNum = $line; } } close($PH); return $versionNum; }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.