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

Greetings to all,

So, in Perl, the following two pieces of code are equivalent:
$output = qx/ls/ ; print $output ;
and
use Win32::Script qw/WScript/ ; $output = WScript('Shell')->Exec("ls")->StdOut->ReadAll ; print $output ;
(except that WScript will use \r\n instead of \n as end of line).

Not so with ActiveState's PerlScript used as the client-sided scripting language in HTML Applications (HTA--a Windows-specific thingy--basically HTML page running on IE on client machine without the security). Consider the following in a HTA page:
<SCRIPT language="PerlScript"> $output = qx/ls/ ; $window->document->write($output) ; </SCRIPT>
and
<SCRIPT language="PerlScript"> use Win32::Script qw/WScript/ ; $output = WScript('Shell')->Exec("ls")->StdOut->ReadAll ; $window->document->write($output) ; </SCRIPT>
qx returned nothing, whereas WScript worked as expected. If I do:
<SCRIPT language="PerlScript"> qx/ls > output.txt/ ; </SCRIPT>
output.txt would be created with results in it, so I know qx did execute the command.

Why such behavior, anyone knows?

Thanks (and thanks to grantm for giving me some pointers).

Replies are listed 'Best First'.
Re: qx vs WScript: Why didn't qx work in my ActiveState's PerlScript?
by jkahn (Friar) on Sep 11, 2002 at 21:52 UTC
    strictly as a guess, I suspect that it's the behavior of qx/ls/ that you don't fully understand.

    qx/ls/ really is telling Perl "drop to a shell and execute the command 'ls', and return the results."
    That does the same thing that the Win32::Script module does but only when you have ls available as a tool!

    On a virgin client Windows machine, you may not have ls available -- I have it on all my systems, because I use Cygwin for nearly everything, but your boss (or your boss's boss) probably doesn't have ls available as a tool on his or her desktop.

    hmm... on reading back through your question, you say that redirecting the output of ls into a text file actually does create a text file. If my suggestion is right, it would be an error message, not the actual results. Perhaps I'm completely off after all...

      I suspect that jkahn is correct. In light of that, you might consider a "pure Perl" solution. Instead of using "ls" (or "dir"), use a combination of opendir(), readdir(), stat(), and closedir() to effect the directory listing. You might also need a chdir() to ensure you're in the right starting place; web servers are notoriously inconsistent about what working directory they use for scripts.

        Sorry for the confusion :p... I meant to use "ls" merely as a generic example. (Maybe I should use "foo"?)... Besides, I do have ls.exe on my Windows (Windows-port of Unix tools). Thanks.
Re: qx vs WScript: Why didn't qx work in my ActiveState's PerlScript?
by Solo (Deacon) on Sep 11, 2002 at 23:04 UTC
    Could be an %ENV problem. Change ls to set and see if the results are the same in each case.

    --
    May the Source be with you.

    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.